我有 Nginx 服务器。目前我有两个项目:
- www.xyz.com -> /var/www/project/public; (此操作有效)
- www.xyz.com/blog -> /var/www/blog;(错误:此网页有重定向循环)
我怎样才能将两者都指向上述文件夹?我尝试了以下代码。
server {
listen 80;
server_name xyz.com;
rewrite ^/(.*)/$ /$1 permanent;
return 301 $scheme://www.xyz.com$request_uri;
}
server {
listen 80 default_server;
server_name www.xyz.com;
rewrite ^/(.*)/$ /$1 permanent;
root /var/www/project/public;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
expires 365d;
}
location ~* \.(pdf)$ {
expires 30d;
}
location ~ \.(?:css|htc|js|js2|js3|js4)$ {
gzip_vary on;
}
# pass the PHP scripts to FastCGI server listening on /var/run/php5-fpm.sock
location ~ \.php$ {
try_files $uri /index.php =404;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location /blog {
root /var/www/blog;
}
location ~ /blog/.+\.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME /var/www/blog$fastcgi_script_name;
}
}
答案1
我认为您应该使用加号 php 部分location /blog
:
location /blog {
alias /var/www/blog;
location ~ \.php$ {
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
而且你不需要location ~ /blog/.+\.php$
。