【问题标题】:Nginx rewrite in subfolder (404)Nginx 在子文件夹中重写 (404)
【发布时间】:2013-07-22 06:00:45
【问题描述】:

我在 NGINX 服务器上有一个站点主机,它曾经可以很好地使用 try_files 在 nginx 站点配置中删除 index.php

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

但是,一旦我将 pretty 永久链接与 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;
}

【问题讨论】:

  • root /web/foo.com 缺少分号
  • @MohammadAbuShady 嗯...对不起我的错,这只是我想隐藏我的真实服务器主机名和文件位置时的错误。

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


【解决方案1】:

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

您可以使用:

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

【讨论】:

  • 非常感谢!我已经尝试了所有其他解决方案,这个对我有帮助。
  • 我花了 5 个小时来解决这个问题。谢谢,谢谢,谢谢。
  • 感谢一百万!这正是我所需要的。
  • 这让我头疼了!!感谢百万及以上投票!
  • 拯救了我的一天!谢谢
【解决方案2】:

嗯...谢谢所有cmets和回答。但最后我用这个方法让它工作了

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
}

如果当前设置有任何问题,请指出。谢谢!

【讨论】:

  • 我得到“这个网页有一个重定向循环”。
  • @Ryan 从此服务器 { } 块中删除任何其他配置,因为它仍然会在此之后读取它们 - 导致无限循环。我通过为博客设置 location /blog { } 和 main index 设置 location / { } 来解决这个问题。
  • 这没有为我正确传递变量。 location /blog/ { try_files $uri $uri/ /blog/index.php?$args; } 工作结束了
  • 您的代码运行良好,但 /wp-admin/ 重定向循环除外。
【解决方案3】:

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

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

【讨论】:

    【解决方案4】:

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

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

    【讨论】:

    • 感谢您的回复。但我按照你的指示,变成了HTTP 500
    • @Cauliturtle 试试这个不同的答案。
    • 然后,当我尝试访问 /blog/post-name 时,Chrome 会尝试将页面下载为文件。
    【解决方案5】:

    试试这个

    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】:

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

      【讨论】:

        【解决方案7】:

        在根目录和一个子文件夹级别的漂亮 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;
        }
        

        【讨论】:

          【解决方案8】:

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

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

          这对我的设置起作用

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

          【讨论】:

            【解决方案9】:

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

            【讨论】:

              猜你喜欢
              • 2014-10-29
              • 2023-03-20
              • 2016-08-16
              • 2023-03-09
              • 1970-01-01
              • 1970-01-01
              • 2021-11-24
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多