简体   繁体   中英

Nginx, Wordpress 3.9.2 and rewrites result in 404

I have never used Nginx before but I wanted to try it as an experiment and I am lost trying to setup pretty URLs up in Wordpress.

However if I setup up Permalinks up to be anything other than query stings, it results in 404's being thrown when try to go to a rewrote URL.

This is my Nginx conf could someone please let me know what I am doing wrong? I've tried multiple answers on similar questions without any luck.

Ngix Conf

server {
    listen 80 default_server;
    listen [::]:80 default_server ipv6only=on;

    root /var/www/;
    index index.php index.html index.htm;

    # Make site accessible from http://localhost/
    server_name site.co.uk;

    client_max_body_size 200M;

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

    # Add trailing slash to */wp-admin requests.
    rewrite /wp-admin$ $scheme://$host$uri/ permanent;

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

    error_page 404 /404.html;

    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
          root /usr/share/nginx/html;
    }

    # pass the PHP scripts to FastCGI server listening on the php-fpm socket
    location ~ \.php$ {
            try_files $uri =404;
            fastcgi_pass unix:/var/run/php5-fpm.sock;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include fastcgi_params;

    }
}

server {
        listen 80;
        server_name www.site.co.uk;
        return 301 $scheme://site.co.uk$request_uri;
}

So for example if I try to go to site.co.uk/my-account this is where the error happens.

Thanks in advance. Matt

I managed to fix it by adding the following rule to my nginx config

if (!-e $request_filename)
{
    rewrite ^(.+)$ /index.php?q=$1 last;
}

From the following website Farinspace - Wordpress nginx rewrite rules

Hope this helps someone else who had the same problem as me.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM