Wordpress Routing not working in Digital Ocean using nginx

1.4k Views Asked by At

I am struggling with this from quite sometime now and thought to ask the community.

I have recently started working on digital ocean and nginx. I was using apache earlier.

I have a simple wordpress website that works fine in local but when pushed on droplet server using nginx the site is not working properly.

The landing page is working fine but when i route to different link I get 404 error page from nginx.

I feel this is because of htaccess file that is not getting picked up in nginx and I have to rewrite rules in nginx but nothing worked.

Here is my configuration file for nginx.conf file, sites-available/default file

sites-available/default

server {
    listen 80;
    server_name martinschildrenacademy.com www.martinschildrenacademy.com;
    return 301 https://$host$request_uri;
    location = /favicon.ico {
            log_not_found off;
            access_log off;
    }

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

    location / {
            # This is cool because no php is touched for static content.
            # include the "?$args" part so non-default permalinks doesn't break when using query string
            try_files $uri $uri/ /index.php?$args;
    }

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

    # Default server configuration
    #
    server {
    #       listen 80 default_server;
    #       listen [::]:80 default_server;
    listen 443 ssl;
    server_name martinschildrenacademy.com www.martinschildrenacademy.com;

    ssl_certificate /etc/letsencrypt/live/martinschildrenacademy.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/martinschildrenacademy.com/privkey.pem;

    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_prefer_server_ciphers on;
    ssl_ciphers 'EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH';

    # SSL configuration
    #
    # listen 443 ssl default_server;
    # listen [::]:443 ssl default_server;
# Note: You should disable gzip for SSL traffic.
    # See: https://bugs.debian.org/773332
    #
    # Read up on ssl_ciphers to ensure a secure configuration.
    # See: https://bugs.debian.org/765782
    #
    # Self signed certs generated by the ssl-cert package
    # Don't use them in a production server!
    #
    # include snippets/snakeoil.conf;

    root /var/www/html;

    # Add index.php to the list if you are using PHP
    index index.php  index.html index.htm index.nginx-debian.html;

    server_name martinschildrenacademy.com;

    # pass PHP scripts to FastCGI server
    #
    location ~ \.php$ {
            include snippets/fastcgi-php.conf;#
    #       # With php-fpm (or other unix sockets):
        fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
    #       # With php-cgi (or other tcp sockets):
    #       fastcgi_pass 127.0.0.1:9000;
    }

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    location ~ /\.ht {
            deny all;
    }
  }

nginx.conf

user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;

events {
    worker_connections 768;
    # multi_accept on;
}

http {

    ##
    # Basic Settings
    ##

    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;
    client_max_body_size 100M;
    # server_tokens off;

    # server_names_hash_bucket_size 64;
    # server_name_in_redirect off;

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

    ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
    ssl_prefer_server_ciphers on;

    ##
    # Logging Settings
    ##

    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;

    ##
    # Gzip Settings
    ##

    gzip on;

    # gzip_vary on;
    # gzip_proxied any;
    # gzip_comp_level 6;
    # gzip_buffers 16 8k;
    # gzip_http_version 1.1;
    # gzip_types text/plain text/css application/json application/javascript text/xml     application/xml application/xml+rss text/javascript;
    ##
    # Virtual Host Configs
    ##

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;
}

Any suggestion that could help me to solve this issue?

3

There are 3 best solutions below

0
On

Try this:

location / {
        try_files $uri $uri/ /index.php?q=$uri&$args;
}
0
On

Just got this working.

Not sure why but I just moved this line

location / {
        # This is cool because no php is touched for static content.
        # include the "?$args" part so non-default permalinks doesn't break when using query string
        try_files $uri $uri/ /index.php?$args;
}

in default.config file from top to bottom server block. And it worked.

1
On

You have two server blocks in your sites-available/default file. The first server block receives HTTP requests and then responds with redirect instructions to the HTTPS version defined with the second server block. You only need to keep the first 3 lines inside the first block and remove the rest like this:

server {
    listen 80;
    server_name martinschildrenacademy.com www.martinschildrenacademy.com;
    return 301 https://$host$request_uri;
}

Nginx configuration for WordPress must have the following location block to work correctly:

location / {
        # This is cool because no php is touched for static content.
        # include the "?$args" part so non-default permalinks doesn't break when using query string
        try_files $uri $uri/ /index.php?$args;
}

So you must insert the location block into the second server block. You did. And it worked.