如何将所有不转到特定子目录的路由重写到nginx中不同的子目录?

我的网站内容(目前保存在/ public_html松散)是在一个Mercurial存储库,所以我希望它有自己的目录。 由于我的wiki位于/ public_html / wiki下,我正在尝试使用路由逻辑将所有不会到mysite.com/wiki的请求路由到/ public_html / content。

目前我有:

server { listen 80; server_name www.mysite.com mysite.com; access_log /www/mysite.com/logs/access.log; error_log /www/mysite.com/logs/error.log; root /www/mysite.com/public_html; location / { index index.html index.htm index.php; } location ~/\.hg { deny all; } location ~ \.php$ { try_files $uri /404.html; include /etc/nginx/fastcgi_params; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /www/mysite.com/public_html$fastcgi_script_name; } } 

我尝试了try_files $uri /content$uri但是这只是让它重写回来了。 我也无法捕捉/维基前缀与位置块,因为我不得不停止模式匹配在这一点上,防止匹配.php。

只要确定$document_root为每个位置和使用这个variables的位置为PHP / fastcgi:

 server { listen 80; server_name www.mysite.com mysite.com; access_log /www/mysite.com/logs/access.log; error_log /www/mysite.com/logs/error.log; location / { root /www/mysite.com/public_html; index index.html index.htm index.php; } location /wiki { root /www/mysite.com/wiki; index index.php; } location ~/\.hg { deny all; } location ~ \.php$ { try_files $uri /404.html; include /etc/nginx/fastcgi_params; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; } } 

这就是为我工作的结果:

 server { listen 80; server_name www.mysite.com mysite.com; access_log /www/mysite.com/logs/access.log; error_log /www/mysite.com/logs/error.log; root /www/mysite.com; location / { try_files /public_html$uri =404; index index.html index.htm index.php; } location /wiki { index index.php; } location ~/\.hg { deny all; } location ~ \.php$ { try_files $uri =404; include /etc/nginx/fastcgi_params; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; } }