【问题标题】:Nginx, Wordpress 3.9.2 and rewrites result in 404Nginx、Wordpress 3.9.2 和重写结果为 404
【发布时间】:2014-08-11 05:59:32
【问题描述】:

我以前从未使用过 Nginx,但我想将它作为一个实验来尝试,但我在尝试在 Wordpress 中设置漂亮的 URL 时迷失了方向。

但是,如果我将永久链接设置为查询字符串以外的任何内容,则会导致在尝试转到重写的 URL 时抛出 404。

这是我的 Nginx conf,有人可以告诉我我做错了什么吗?我在类似问题上尝试了多个答案,但没有任何运气。

Ngix 会议

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;
}

例如,如果我尝试访问 site.co.uk/my-account,这就是发生错误的地方。

提前致谢。 马特

【问题讨论】:

标签: php wordpress nginx url-rewriting


【解决方案1】:

我设法通过将以下规则添加到我的 nginx 配置来修复它

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

来自以下网站Farinspace - Wordpress nginx rewrite rules

希望这可以帮助和我遇到同样问题的其他人。

【讨论】:

    猜你喜欢
    • 2023-03-08
    • 2017-04-06
    • 2015-11-20
    • 2016-05-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-10
    • 2019-11-24
    相关资源
    最近更新 更多