18

我在NGINX服务器上有一个站点主机,它曾经可以正常工作,可以index.php使用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;
}
4

9 回答 9

42

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

您可以使用:

location /blog/ {
    index index.php;
    try_files $uri $uri/ /blog/index.php?$args;
}
于 2014-06-10T04:49:13.893 回答
17

嗯...感谢您的所有评论和回答。但最后我用这个方法让它工作

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
}

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

于 2013-07-23T16:42:15.847 回答
5

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

location /blog {
    index index.php;
    try_files $uri $uri/ /blog/index.php?$args;
}
于 2015-05-13T05:06:05.660 回答
1

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

location ~ /blog(.*) {
    index index.php;
    try_files $uri /blog/index.php?$1&$args;
}
于 2013-07-23T09:08:11.967 回答
1

尝试这个

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;
    }
}
于 2015-12-09T03:00:56.473 回答
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;
}
于 2015-10-16T17:00:13.680 回答
0

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

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

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

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

location / {
        try_files $uri $uri/ $realdir/index.php?$args;
}
于 2017-03-12T14:20:01.517 回答
0

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

  1. 仅重写,没有提供任何静态文件
  2. 只有尝试文件,永久链接不起作用

这适用于我的设置

location /blog/ {
      rewrite ^/blog/(blog/(tag|category|20??)/.*)+$ /blog/index.php?$1;
      try_files $uri $uri/ /blog/index.php?$args =404;
}
于 2018-05-15T08:36:37.207 回答
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;

}
于 2020-06-05T02:13:29.610 回答