【问题标题】:How can I make nginx rewrite play nice with alias?如何使 nginx rewrite 与别名一起使用?
【发布时间】:2014-08-30 15:40:55
【问题描述】:

类似于this question,我遇到了 nginx 重写的问题。

这是我的 nginx 配置文件:

server {                                                                           
    listen 80;                                                                     
    server_name myserver.com www.myserver.com;                               
    index index.html;                                                              
    root /var/www/myserver.com/site/;                                           

    location /blog {                                                               
        alias /var/www/myserver.com/blog/output/;                               
        break;                                                                     
    }                                                                              

    location / {                                                                   
        autoindex on;                                                              
        try_files $uri $uri/ @htmlext;                                             
    }                                                                              

    location /.hg {                                                                
        deny all;                                                                  
        return 404;                                                                
    }                                                                              

    location ~ \.html$ {                                                           
        try_files $uri =404;                                                       
    }                                                                              

    location @htmlext {                                                            
        rewrite ^(.*)$ $1.html last;                                               
    }                                                                              

    error_page 500 502 503 504 /media/50x.html;                                    
}                                    

看起来问题出在location ~ \.html 规则上。当我删除它时,它工作正常。

我尝试了多种方法,例如:

  • location /blog 指令移到顶部(它曾经位于底部)
  • 按照链接问题的建议输入break;

到目前为止,似乎没有任何效果。在当前配置中,我得到一个 404,这也是 access.log 中显示的内容。

如何使重写工作隐藏.html 扩展,同时让我提供blog/output/ 内容?

【问题讨论】:

    标签: nginx rewrite alias


    【解决方案1】:

    这个问题最奇怪的答案很可能来自this answer

    location ^~ /blog {
            alias /var/www/myserver.com/blog/output/;                               
            break;
    }
    

    老实说,即使在阅读了documentation 之后,我仍然无法解释为什么会这样。我知道这与^~ 的正则表达式匹配行为有关,但这就是我所拥有的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-06-09
      • 2014-08-16
      • 1970-01-01
      • 2019-12-18
      • 2012-01-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多