大网数据广告活动
服务器运维问答

url rewrite, nginx throw 404 errors, nothing in error logs

· 52CE 我爱测

below rewrite works fine: rewrite ^/city/restaurants$ /city/listings/restaurants permanent; but this doesnt work rewrite ^/city/restaurants$ /city/listings/restaurants last; rewrite ^/city/restaurants$ /city/listings/restaurants …

问题原文

原帖链接 · Olaf Stavenger · CC BY-SA 3.0

below rewrite works fine:

rewrite ^/city/restaurants$ /city/listings/restaurants permanent;

but this doesnt work

rewrite ^/city/restaurants$ /city/listings/restaurants last;
rewrite ^/city/restaurants$ /city/listings/restaurants break;

what am i doing wrong?

below entire nginx server block

  server {
          listen 80 default backlog=1024;
          server_name mydomain.com;


          client_max_body_size 20M;
          charset utf-8;
          keepalive_timeout 50;

          access_log /var/log/access_log main;
          error_log /var/log/error_log info;


          root /var/www/;
          index index.php index.phtml index.html;
  autoindex on;

   location ~ \..*/*\.php$ {
       return 403;
   }
   location ~^/sites/.*/private/{
       return 403;
   }
   location ~^/sites/.*/files/* {
       try_files $uri @rewrite;
   }
   location ~ (^|/)\. {
       return 403;
   }

   location / {

rewrite ^/city/restaurants$ /city/listings/restaurants last;

    location ~* \.php$ {
                            include /etc/nginx/fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                            #fastcgi_pass unix:/var/run/php5-fpm.sock;
                            fastcgi_pass 127.0.0.1:9000;
                            #try_files $uri @rewrite;
                            }
                    try_files $uri @rewrite;
                    }

            location @rewrite {
                    rewrite ^ /index.php;
            }
            location ~* \.(?:ico|css|js|gif|jpg|jpeg|png)$
            {
                    try_files $uri $uri/;
                    expires 30d;

             }
}

已采纳解答

原帖链接 · Mark Stosberg · CC BY-SA 3.0

First, move the key rewrite out of the location block into the enclosing server block. That will simplify your logic.

Next, enable rewrite debug logging to confirm if the 404 you are getting is from the original URL, or the rewritten URL.

Finally, if the root cause is still not found enable general debug logging for Nginx:

error_log /path/to/log debug;

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2016-01-13)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。