wordpressamazon-web-servicesnginxnginx-config

Nginx Setup for WordPress: Facing 301 Redirect Issues


We are planning to move away from Apache/Httpd to Nginx for our wordpress site. I thought the challenge for me would only be while adding the .htaccess codes in nginx configuration but now I am getting 301 redirect from the site whenever I enable nginx.

I am trying to keep the setup simple for now just to see my homepage coming live. Please find the configuration file below.

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 4096;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    include /etc/nginx/conf.d/*.conf;

    server {
        listen       80;
        listen       [::]:80;
        server_name  _;
        root         /var/www/html/;

        include /etc/nginx/default.d/*.conf;

        error_page 404 /404.html;
        location = /404.html {
        }
        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
        }
    }

I have setup SSL redirection from AWS ALB at the moment.

It is able to access my healthcheck file but not main wordpress site

Issue: The main WordPress site is not accessible, but it works fine when using Apache/Httpd. There are no apparent issues in the error logs.

What am I missing in the configuration? Any help is appreciated!


Solution

  • Thanks to the answer above that I got start and which also started me to show the light at the end of the tunnel since I was able to my site up and running.

    But it was not a solution for me since I had to make Nginx work with ALB SSL/443 listener.

    The solution was pretty much easier and got it on Reddit at the end.

    Solution: Add the following line in wp-config.php above require_once(ABSPATH . 'wp-settings.php');:

     /* Turn HTTPS 'on' if HTTP_X_FORWARDED_PROTO matches 'https' */
    if (strpos($_SERVER['HTTP_X_FORWARDED_PROTO'], 'https') !== false) {
        $_SERVER['HTTPS'] = 'on';
    }
    

    The full explanation of the issue can be found here