Jay
Jay

Reputation: 2244

WordPress + Nginx is giving me a "403 forbidden" error

I am trying to setup WordPress "installed using docker image" on Nginx server.

I run the docker image using the following docker-compose.yml file

version: "3"
services:

    wordpress_app:
        image: wordpress:latest
        restart: always
        container_name: wordpress_app
        environment:
            WORDPRESS_DB_HOST: mysql_server:3306
            WORDPRESS_DB_USER: db_username
            WORDPRESS_DB_PASSWORD: db_password
            WORDPRESS_DB_NAME: db_name
        depends_on:
            - mysql_server
        volumes:
            - /data/wordpress_app/public_html:/var/www/html
        ports:
            - 8000:80
            - 8001:443

    mysql_server:
        image: mysql:latest
        restart: always
        container_name: mysql_server
        environment:
            MYSQL_DATABASE: db_name
            MYSQL_USER: db_username
            MYSQL_PASSWORD: db_password
            MYSQL_ROOT_PASSWORD: root_password
        volumes:
            - mysql_server_data:/var/lib/mysql

volumes:
    mysql_server_data:

networks:
    default:
       external:
           name: nginx-network

Now, I am trying to proxy the public domain usa.mydomain.com to localhost:8000

server {
    listen 80;
    server_name usa.mydomin.com;

    root /var/www/html;

    access_log off;
    error_log /var/log/nginx/usa.mydomain.com-error.log;

    index index.html index.php;

    location / {
        # First attempt to serve request as file, then as directory, then fall back to displaying a 404.
        try_files $uri $uri/ /index.php?$args;
    }


    # pass the PHP scripts to FastCGI server listening on localhost:8000
    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass localhost:8000;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param SCRIPT_NAME $fastcgi_script_name;
    }
}

However, when I browse usa.mydomain.com I get

403 Forbidden nginx/1.14.0 (Ubuntu)

How can I correct this error?

I changed the Nginx to the following configuration

server {
    listen 80;
    server_name usa.mydomin.com;

    #root /var/www/html; # <<<<< REMOVED THIS
    root /data/wordpress_app/public_html; # <<<<< ADDED THIS

    access_log off;
    error_log /var/log/nginx/usa.mydomain.com-error.log;

    index index.html index.php;

    location / {
    # First attempt to serve request as file, then as directory, then fall back to displaying a 404.
        try_files $uri $uri/ /index.php?$args;
    }

    # pass the PHP scripts to FastCGI server listening on localhost:8000
    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass localhost:8000;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param SCRIPT_NAME $fastcgi_script_name;
    }
}

But now I am getting

502 Bad Gateway

looking at the logs I get the following error

2020/10/16 15:22:58 [error] 1475#1475: *68 upstream sent unsupported FastCGI protocol version: 72 while reading response header from upstream, client: MyIpAddress, server: usa.mydomain.com, request: "GET / HTTP/1.1", upstream: "fastcgi://[::1]:8000", host: "usa.mydomain.com"

2020/10/16 15:22:58 [error] 1475#1475: *68 upstream sent unsupported FastCGI protocol version: 72 while reading response header from upstream, client: MyIpAddress, server: usa.mydomain.com, request: "GET /favicon.ico HTTP/1.1", upstream: "fastcgi://127.0.0.1:8000", host: "usa.mydomain.com", referrer: "http://usa.mydomain.com/"

How can I fix this issue?

Upvotes: 0

Views: 3850

Answers (2)

Saqib
Saqib

Reputation: 109

My Specific Problem: I was getting 403 for only SearchWP plugin's assets, after installing SearchWP plugin through composer. I am using Docksal for local docker based WordPress development.

Solution:

  1. Opened shell into service's container and navigated to the plugins directory and checked permissions. Related commands: fin bash and ls -la
  2. Used chmod -R 755 searchwp to fix the target directory's permission and it resolved the issue.

Hopefully, it might help someone.

Thanks

Upvotes: 0

Jay
Jay

Reputation: 2244

After few day of trying the app was fixed using the following settings. I wish this saves someone time

server {
    listen 80;
    server_name usa.mydomain.com;

    root /data/wordpress_app/public_html;

    access_log off;

    error_log /var/log/nginx/wordpress_app-error.log;

    index index.html index.php;

    location = /favicon.ico {
        log_not_found off;
        access_log off;
    }

    location = /robots.txt {
        allow all;
        log_not_found off;
        access_log off;
    }

    location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
        expires max;
        log_not_found off;
    }

    location / {

        try_files $uri $uri/ /index.php?$args;
        proxy_pass http://localhost:8000;
        proxy_set_header HOST $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}

Upvotes: 2

Related Questions