我的网站内容(目前大致保存在 /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
,但那只会让它通过重写重新循环。我也无法用位置块捕获 /wiki 前缀,因为我必须停止此时的模式匹配,从而阻止 .php 被匹配。
答案1
只需确定$document_root
每个位置并在 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;
}
}
答案2
这最终为我工作:
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;
}
}