Nginx自定义配置

问题描述:

我需要我的NGINX配置根据URL做一些独特的事情。Nginx自定义配置

如果网址是:

mydomain.com/blog OR mydomain.com/blog/

我需要NGINX就可以提供端口2368鬼博客我已经做到了这一点:

location /blog/ { 
    proxy_pass http://localhost:2368; 
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
    proxy_set_header Host $http_host; 
    proxy_set_header X-Forwarded-Proto $scheme; 
    proxy_buffering off; 
} 

如果网址是:

mydomain.com/some-article-title

我需要NGINX引导至some-article-title.html。我已经做到了这一点:

location/{ 
    try_files $uri $uri/ $uri.html =404; 
} 

第一个问题:我也想这个工作如果URL是mydomain.com/some-article-title/或者如果情况是不同的,例如mydomain.com/Some -article-标题。我如何在NGINX中做到这一点?

第二个问题:如果没有匹配的HTML文件,我还需要NGINX重定向到一个特定的子域名,例如,如果URL是mydomain.com/jimsmith并且没有HTML文件jimsmith.html我需要NGINX带用户到jimsmith.mydomain.com我该怎么做?

location /blog/将不符合请求/blog,但location /blog会。

try_files ...尝试文件。因此,如果您的请求包含尾部斜线,nginx将搜索目录(以/结尾的文件仅在任何文件系统上为目录)。

如果您希望nginx在请求包含尾部斜线时搜索文件,则需要rewrite请求其前斜线格式。请注意,目录将不再可用...所以下面显示的最通用的形式远不被推荐。该斜线一种重写不鼓励在所有以任何形式反正...

location ~* ^(?<article>/[^/]+)/$ { 
    return 302 $scheme://$host$article; 
} 

你不能有同名几个文件以信函文件系统中的大写与否,如some-article-titleSome-article-Title,所以搜索任何名称将始终与现有文件成功。

如果要重定向第一个参数不匹配try_files的文件,最后一个参数可用作回退到指定位置,您可以在其中发出重定向(请参阅try_files文档)。

location/{ 
    try_files $uri $uri/ $uri.html @notexisting; 
} 

location @notexisting { 
    rewrite ^/(?<subdomain>.*)$ $scheme://$subdomain.mydomain.com; 
}