wordpress Nginx 在子文件夹中重写 (404)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17805576/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 19:17:39  来源:igfitidea点击:

Nginx rewrite in subfolder (404)

wordpressnginxhttp-status-code-404

提问by Cauliturtle

I has a site host on a NGINXserver which used to work fine to remove index.phpin nginx site config using try_files.

我在NGINX服务器上有一个站点主机,它曾经可以正常工作,可以index.php使用try_files.

But now I am going to add a blog on it, where the URL will be www.foo.com/blog, I can access the blog and use index.php?p=.

但是现在我要在上面添加一个博客,URL 所在的位置www.foo.com/blog,我可以访问该博客并使用index.php?p=.

But, once I use prettypermalink with Nginx Helper, www.foo.com/blog/2013/07/bar, I get 404.

但是,一旦我在 Nginx Helper 中使用了漂亮的永久链接www.foo.com/blog/2013/07/bar,我就会得到404.

server {
  # don't forget to tell on which port this server listens
  listen 80;

  # listen on the www host
  server_name foo.com;

  # and redirect to the non-www host (declared below)
  return 301 $scheme://www.ultra-case.com$request_uri;
}

server {
  # listen 80 default_server deferred; # for Linux
  # listen 80 default_server accept_filter=httpready; # for FreeBSD
  listen 80;

  # The host name to respond to
  server_name www.foo.com;

  # Path for static files
  root /web/foo.com

  #index file
  index index.php;

  #Specify a charset
  charset utf-8;

  # Custom 404 page
  error_page 404 /404.html;

  # Uri Rewrite

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

  location / {
    autoindex on;
    # This is cool because no php is touched for static content.
    # include tihe "?$args" part so non-default permalinks doesn't break when using query string
    try_files $uri $uri/ /index.php?$args;
  }
  location ~ \.php$ {
    #NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
    include fastcgi.conf;
    fastcgi_intercept_errors on;
    fastcgi_pass 127.0.0.1:9000;
    fastcgi_index index.php;
  }

  # Include the component config parts for h5bp
  include conf/h5bp.conf;
}

回答by ereckers

The accepted answer routes everything through index.php.
This will break certain script includes, the wp-adminscript being one of them.

接受的答案通过index.php.
这将破坏某些脚本包含,wp-admin脚本就是其中之一。

You can use:

您可以使用:

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

回答by Cauliturtle

Um... Thank you for all comments and answer. But finally I use this method to get it works

嗯……谢谢大家的评论和回答。但最后我用这个方法让它工作

location /blog {
    index index.php;
    rewrite ^/blog/(.*)+$ /blog/index.php?; # it finally works
    # return 200 $request_uri; # it is for inspect what $request_uri is
    # try_files $uri $uri/ /blog/index.php$request_uri$is_args$args; # it gets 500 server error
}

Please point out if current setting has any problems. thank you!

请指出当前设置是否有任何问题。谢谢你!

回答by Nicolas Guérinet

I would suggest the following, to catch any permalinks under subfolder /blog

我会建议以下内容,以捕获子文件夹 /blog 下的任何永久链接

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

回答by Mohammad AbuShady

Try this, I changed my answer to try to imitate the same behaviour you are using in your rewrite.

试试这个,我改变了我的答案,试图模仿你在重写中使用的相同行为。

location ~ /blog(.*) {
    index index.php;
    try_files $uri /blog/index.php?&$args;
}

回答by Jesús Germade

Think for php, rewrite is no needed with something like this:

想想php,不需要像这样重写:

location /app/ {
  include fastcgi_params;
  fastcgi_param  SCRIPT_FILENAME /path/to/your/app/index.php;
  fastcgi_pass php;
}

With following fastcgi pass

随着以下fastcgi通行证

upstream php {
    server unix:/var/run/php5-fpm.sock;
}

回答by jk2K

Try this

尝试这个

location /api {
    # example: http://demo.com/api/channels/dmzb
    root   /data/webserver/demo.com/api/web;
    rewrite ^/api/(.*) / break;
    try_files $uri $uri/ /api/index.php?$args;

    location ~ ^/api/index\.php {
        fastcgi_pass  127.0.0.1:9000;
        fastcgi_index index.php;
        include fastcgi.conf;

        # fix request_uri
        set $changed_request_uri $request_uri;
        if ($changed_request_uri ~ ^/api(.*)) {
            set $changed_request_uri ;
        }
        fastcgi_param REQUEST_URI $changed_request_uri;

        # fix script_filename
        fastcgi_split_path_info ^(?:\/api\/)(.+\.php)(.*);
        fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;
    }
}

回答by amq

A universal solution for pretty URLs in root and one subfolder level:

根目录和一个子文件夹级别的漂亮 URL 的通用解决方案:

set $virtualdir "";
set $realdir "";

if ($request_uri ~ ^/([^/]*)/.*$ ) {
        set $virtualdir /;
}

if (-d "$document_root$virtualdir") {
        set $realdir "${virtualdir}";
}

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

回答by paulusm

I found that with permalink enabled, I needed a combination of both sets of answers given here, otherwise

我发现启用永久链接后,我需要结合此处给出的两组答案,否则

  1. With only the rewrite, none of the static files got served
  2. With only the try files, the permalinks did not work
  1. 只有重写,没有静态文件得到服务
  2. 只有尝试文件,永久链接不起作用

This is working on my set up

这是我的设置

location /blog/ {
      rewrite ^/blog/(blog/(tag|category|20??)/.*)+$ /blog/index.php?;
      try_files $uri $uri/ /blog/index.php?$args =404;
}