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

时间:2013-07-23 08:52:23

标签: wordpress nginx http-status-code-404

我在 NGINX 服务器上有一个站点主机,它曾使用index.php在nginx站点配置中正常删除try_files

但现在我要在其上添加一个博客,其中的网址为www.foo.com/blog,我可以访问博客并使用index.php?p=

但是,一旦我使用漂亮的永久链接与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;
}

9 个答案:

答案 0 :(得分:36)

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

您可以使用:

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

答案 1 :(得分:15)

嗯...谢谢你的评论和回答。但最后我用这种方法让它起作用

location /blog {
    index index.php;
    rewrite ^/blog/(.*)+$ /blog/index.php?$1; # 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
}

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

答案 2 :(得分:5)

我建议以下内容,以捕获子文件夹/博客

下的任何永久链接
location /blog {
    index index.php;
    try_files $uri $uri/ /blog/index.php?$args;
}

答案 3 :(得分:1)

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

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

答案 4 :(得分:0)

想想php,用这样的东西不需要重写:

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

使用以下fastcgi传递

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

答案 5 :(得分:0)

试试这个

location /api {
    # example: http://demo.com/api/channels/dmzb
    root   /data/webserver/demo.com/api/web;
    rewrite ^/api/(.*) /$1 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 $1;
        }
        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;
    }
}

答案 6 :(得分:0)

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

data-parsley-length="[10, 10]"

答案 7 :(得分:0)

我发现启用固定链接后,我需要将这两组答案组合在一起,否则

  1. 只有重写,没有提供任何静态文件
  2. 仅使用try文件,永久链接无法正常工作
  3. 这正在我的设置

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

答案 8 :(得分:0)

ip网址:123.123.123 / xxxxxxxxxx /

location /xxxxxxxxxx/ {
   try_files $uri $uri/ /xxxxxxxxxx/index.php?$query_string;
}

# Rewrite multisite '.../wp-.*' and '.../*.php'.
if (!-e $request_filename) {
    rewrite ^(/xxxxxxxxxx/.*)+(/wp-.*) /xxxxxxxxxx/$2 last;
    rewrite ^(/xxxxxxxxxx/.*)+.*(/wp-admin/.*\.php)$ /xxxxxxxxxx/$2 last;
    rewrite ^(/xxxxxxxxxx/.*)+(/.*\.php)$ /xxxxxxxxxx/$2 last;

}