更改永久链接在Nginx上给我404错误


18

编辑

事实证明,由于nginx不使用它,所以我试图编辑.htaccess时出错了。我显然需要做的是编辑.conf文件。在我阅读本文之前,my_app.conf看起来像这样:

upstream backend {
    server unix:/u/apps/my_app/tmp/php.sock;
}

server {

    listen 80 default;
    root /u/apps/my_app/www;
    index index.php;

    access_log /u/apps/my_app/logs/access.log;
    error_log /u/apps/my_app/logs/error.log;

    location / {
        try_files $uri $uri/ /index.php;
    }

    # This location block matches anything ending in .php and sends it to
    # our PHP-FPM socket, defined in the upstream block above.
    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_pass backend;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME /u/apps/my_app/www$fastcgi_script_name;
        include fastcgi_params;
    }

    # This location block is used to view PHP-FPM stats
    location ~ ^/(php_status|php_ping)$ {
        fastcgi_pass backend;
        fastcgi_param SCRIPT_FILENAME $fastcgi_script_name;
        include fastcgi_params;
        allow 127.0.0.1;
        deny all;
    }

    # This location block is used to view nginx stats
    location /nginx_status {
        stub_status on;
        access_log off;
        allow 127.0.0.1;
        deny all;
    }
}

现在看起来像这样,但仍然无法正常工作:

upstream backend {
    server unix:/u/apps/my_app/tmp/php.sock;
}

server {

    listen 80 default;
    root /u/apps/my_app/www;
    index index.php;

    access_log /u/apps/my_app/logs/access.log;
    error_log /u/apps/my_app/logs/error.log;

    location / {
        try_files $uri $uri/ /index.php;
    }

    location /wordpress/ {
        try_files $uri $uri/ /index.php?$args;
    }

    rewrite /wp-admin$ $scheme://$host$uri/ permanent;

    location ~* ^.+\.(ogg|ogv|svg|svgz|eot|otf|woff|mp4|ttf|rss|atom|jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2    |doc|xls|exe|ppt|tar|mid|midi|wav|bmp|rtf)$ {
       access_log off; log_not_found off; expires max;
    }

    # Uncomment one of the lines below for the appropriate caching plugin (if used).
    #include global/wordpress-wp-super-cache.conf;
    #include global/wordpress-w3-total-cache.conf;

    # This location block matches anything ending in .php and sends it to
    # our PHP-FPM socket, defined in the upstream block above.
    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_pass backend;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME /u/apps/my_app/www$fastcgi_script_name;
        include fastcgi_params;
    }

    # This location block is used to view PHP-FPM stats
    location ~ ^/(php_status|php_ping)$ {
        fastcgi_pass backend;
        fastcgi_param SCRIPT_FILENAME $fastcgi_script_name;
        include fastcgi_params;
        allow 127.0.0.1;
        deny all;
    }

    # This location block is used to view nginx stats
    location /nginx_status {
        stub_status on;
        access_log off;
        allow 127.0.0.1;
        deny all;
    }
}

有人知道我在做什么错吗?

结束编辑

我已将永久链接从默认更改为/%postname%/,现在​​WordPress的“管理”面板中的链接给我404错误-不是WordPress 404页面,nginx 404页面。查找为什么这告诉我应该编辑我的.htaccess文件,或者告诉我WordPress无法重写.htaccess-该.htaccess文件不存在,并且当我更改固定链接时WordPress没有给出任何错误。

我试图在我的wordpress文件夹中创建一个空白的.htaccess文件,给它666的权限,将用户和组更改为www-data,然后更改永久链接-不起作用。然后,在更改永久链接之前,将其更改为此:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

如果没有工作,我改RewriteBase/wordpress/然后再次更改了永久链接-仍然没有。

我还进入了站点的.conf文件,并更改try_files $uri $uri/ /index.php;为以下内容,每次都重新启动nginx和php5-fpm;

try_files $uri $uri/ /index.php?$query_string;

try_files $uri $uri/ /index.php?q=$request_uri;

try_files $uri $uri/ /index.php?$args;

我正在使用Nginx运行家庭服务器。关于这里发生的事情有什么想法吗?

Answers:


13

这些是Apache .htaccess重写规则,但是您已经声明自己在Nginx服务器上。Nginx不使用.htaccess类似目录的文件,更不用说使用.htaccess文件本身了。您需要编辑服务器配置本身。食品法典有一个详细的样本

# WordPress single blog rules.
# Designed to be included in any server {} block.

# This order might seem weird - this is attempted to match last if rules below fail.
# http://wiki.nginx.org/HttpCoreModule
location / {
    try_files $uri $uri/ /index.php?$args;
}

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

# Directives to send expires headers and turn off 404 error logging.
location ~* ^.+\.(ogg|ogv|svg|svgz|eot|otf|woff|mp4|ttf|rss|atom|jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|tar|mid|midi|wav|bmp|rtf)$ {
       access_log off; log_not_found off; expires max;
}

# Uncomment one of the lines below for the appropriate caching plugin (if used).
#include global/wordpress-wp-super-cache.conf;
#include global/wordpress-w3-total-cache.conf;

# Pass all .php files onto a php-fpm/php-fcgi server.
location ~ [^/]\.php(/|$) {
    fastcgi_split_path_info ^(.+?\.php)(/.*)$;
    if (!-f $document_root$fastcgi_script_name) {
        return 404;
    }
    # This is a robust solution for path info security issue and works with "cgi.fix_pathinfo = 1" in /etc/php.ini (default)

    include fastcgi.conf;
    fastcgi_index index.php;
#   fastcgi_intercept_errors on;
    fastcgi_pass php;
}

谢谢,如果我有声誉,我会投票赞成。我在将它实现到.conf文件中时遇到了一些麻烦,因为它已经比默认设置有了很大的变化,但是至少我不再摆弄.htaccess了。
ninjachicken15年

@s_ha_dum,直到昨天我更新到wordpress 4.8时,我才使用thies的配置,现在我在永久链接自定义结构上得到了404s。...尝试从昨天开始调试它,但是没有问题,没有任何想法吗?
Jadeye

我不得不将最后一行更改为“ fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;”。可以在Ubuntu 18.04上运行,但是它可以正常工作并节省了我的理智
Rob

18

我正在使用具有自定义永久链接设置的wordpress多站点:/%category%/%postname%/

/etc/nginx/site-available/domain.conf

在服务器上{

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

如果您的root wordpress不是webroot,而是http://domain.com/wordpress/

location /wordpress/ {
    try_files $uri $uri/ /wordpress/index.php?q=$uri$args;
}

如果您将旧的wordpress与blogs.dir一起使用,请添加:location ^〜/blogs.dir {internal; 别名/var/www/wordpress/wp-content/blogs.dir; access_log关闭;log_not_found关闭; 最多到期;}

检查nginx配置:sudo nginx -t

重新加载nginx:sudo服务nginx重新加载

另外,请尝试更改永久链接设置。


4
对于想要手动将WordPress安装移动到新域名下的子目录的任何人来说,这都是最佳答案!非常感谢!这应该是公认的答案。
specialk1st

1
路径:/ etc / nginx / site-available /应该显示为:/ etc / nginx / sites-available /
Grant

6

必须将这段代码添加到/sites-available/your-settings-file和中/sites-enabled/your-settings-file

server {
[...]

if (!-e $request_filename) {
    rewrite ^.*$ /index.php last;
}

[...]
}

现在为我工作。


1
这是我一直在寻找的简单答案,谢谢
ThEBiShOp

1
这工作了!您能解释一下它的作用吗?(尤其是“最后一个”部分)
。– Sidd

1

我必须将根路径设置为wordpressś安装目录:root / var / www / html / wp;

我不喜欢它,因为我在这台计算机上安装了更多应用程序,但是创建更多虚拟主机就足够了。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.