我正在将几个站点从Apache迁移到Nginx,我碰到了一个拥有虚拟目录(别名)的网站。 别名本身工作正常,适当的文件得到服务,但PHP代理不正确。
server { listen 443 default_server ssl; server_name dev.myproject.mydomain.net; root /opt/dev/project-root; index index.php; ssl_certificate /etc/ssl/certs/server.crt; ssl_certificate_key /etc/ssl/certs/server.pem; access_log /var/log/nginx/vh.project.access.log; error_log /var/log/nginx/vh.project.error.log; location ~ ^/alias_name/(.*) { alias /opt/dev/project-root/www/$1; location ~ ^/alias_name/(.+\.php)$ { alias /opt/dev/project-root/www/$1; include /etc/nginx/conf/php; } } location ~ \.php$ { include /etc/nginx/conf/php; } }
我还没有尝试过,但我怀疑,如果我将\.php$位置块的内容复制到别名块中,事情会正常工作,但是我的工程师讨厌复制。 有什么办法可以避免这种情况(假设它可以工作)? 直觉上,似乎两个位置块将被评估。
我敢肯定,我也可以将php块移出一个单独的文件并包含它。
任何build议最好的(阅读:大多数nginx-y)的方式来处理这将是非常感激。
UPDATE
基于凯尔的回答,我已经把PHP代理移出到一个单独的conf/目录。 conf/php文件有这样的代码:
location ~ \.php$ { fastcgi_split_path_info ^(.+\.php)(/.+)$; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; include fastcgi_params; }
我的别名位置块现在看起来像这样:
location ~ ^/alias_name/(.*) { alias /opt/dev/path/to/aliased/$1; include /etc/nginx/conf/php; }
如果我放置一个静态HTML文件在别名,它渲染罚款。 一个PHP文件返回一个404。
UPDATE
在@ quanta的帮助下,我已经设法正确地使用PHP,并且使用config更新了别名位置块。 不幸的是,如果URL包含别名,则不会呈现静态内容。 今天不是我的一天。
好吧,所以我可能已经通过暴力试验和错误发现了我自己的答案,一旦我join@ quanta的信息(见上面的评论)。 这个虚拟主机服务器块似乎正确地服务于PHP 和静态内容:
server { listen 443 default_server ssl; server_name dev.myproject.mydomain.net; root /opt/dev/project-root; index index.php; ssl_certificate /etc/ssl/certs/server.crt; ssl_certificate_key /etc/ssl/certs/server.pem; access_log /var/log/nginx/myproject.dev.access.log; error_log /var/log/nginx/myproject.dev.error.log; location ~ ^/alias_name/(.+\.php)$ { alias /opt/dev/project-root/www/$1; include /etc/nginx/conf/php; } location ~ ^/alias_name/(.*) { alias /opt/dev/project-root/www/$1; } location ~ \.php$ { include /etc/nginx/conf/php; } }
我不知道我是否会碰到问题,我不能说我完全理解这个差异,但是简单地去掉嵌套的location块似乎已经成功了。
你可以把PHP块放在一个外部包含,然后做到这一点,我不是100%的包括命令,但这应该工作。
server { listen 443 default_server ssl; server_name dev.myproject.mydomain.net; root /opt/dev/project-root; index index.php; ssl_certificate /etc/ssl/certs/server.crt; ssl_certificate_key /etc/ssl/certs/server.pem; access_log /var/log/nginx/vh.project.access.log; error_log /var/log/nginx/vh.project.error.log; location ~ ^/alias_name/(.*) { alias /opt/dev/path/to/aliased/$1; include php.inc } # include defualt location include php.inc }
php.inc
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # location ~ \.php$ { fastcgi_split_path_info ^(.+\.php)(/.+)$; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; include fastcgi_params; }