带有多个位置块的nginxconfiguration

我正在尝试configurationnginx从2个不同的位置提供2个不同的PHP脚本。 configuration如下。

  1. 我有一个Laravel安装,位于/home/hamed/laravel ,其中应该提供public目录。
  2. 我在/home/hamed/www/blog安装了Wordpress。

这是我的nginxconfiguration:

 server { listen 443 ssl; server_name example.com www.example.com; #root /home/hamed/laravel/public; index index.html index.htm index.php; ssl_certificate /root/hamed/ssl.crt; ssl_certificate_key /root/hamed/ssl.key; location /blog { root /home/hamed/www/blog; try_files $uri $uri/ /blog/index.php?do=$request_uri; } location / { root /home/hamed/laravel/public; try_files $uri $uri/ /index.php?$request_uri; } error_page 404 /404.html; error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } location ~ \.php$ { try_files $uri =404; fastcgi_split_path_info ^(.+\.php)(/.+)$; #fastcgi_pass 127.0.0.1:9000; fastcgi_pass unix:/var/run/php5-fpm.hamed.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } } 

问题是当试图通过调用example.com/blog来访问wordpress部分仍然laravel installtion接pipe请求。

现在我试图用aliasreplacelocation块内的root指令无济于事。

根据本指南的index指令或try_files里面的location触发内部redirect,我怀疑会导致此行为。

请有人帮我弄清楚这个?

问题是, location ~ \.php$ { ... }负责处理所有的php脚本,这些脚本分为两个不同的根。

一种方法是使用server容器的通用root ,并在每个前缀位置块内执行内部重写。 就像是:

 location /blog { rewrite ^(.*\.php)$ /www$1 last; ... } location / { rewrite ^(.*\.php)$ /laravel/public$1 last; ... } location ~ \.php$ { internal; root /home/hamed; ... } 

以上应该工作(但我没有测试过你的情况)。

第二种方法是使用嵌套的位置块。 location ~ \.php$ { ... }块然后被复制到每个应用程序的位置块中。 就像是:

 location /blog { root /home/hamed/www; ... location ~ \.php$ { ... } } location / { root /home/hamed/laravel/public; ... location ~ \.php$ { ... } } 

现在,一个已经测试工作。

感谢@RichardSmith,我终于设法创建了正确的配置。 这是最后的工作配置。 我不得不使用嵌套的location块和反向正则表达式匹配组合工作。

 server { listen 443 ssl; server_name example.com; root /home/hamed/laravel/public; # index index.html index.htm index.php; ssl_certificate /root/hamed/ssl.crt; ssl_certificate_key /root/hamed/ssl.key; location ~ ^/blog(.*)$ { index index.php; root /home/hamed/www/; try_files $uri $uri/ /blog/index.php?do=$request_uri; location ~ \.php$ { try_files $uri =404; fastcgi_split_path_info ^(.+\.php)(/.+)$; #fastcgi_pass 127.0.0.1:9000; fastcgi_pass unix:/var/run/php5-fpm.hamed.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } } location ~ ^((?!\/blog).)*$ { #this regex is to match anything but `/blog` index index.php; root /home/hamed/laravel/public; try_files $uri $uri/ /index.php?$request_uri; location ~ \.php$ { try_files $uri =404; fastcgi_split_path_info ^(.+\.php)(/.+)$; #fastcgi_pass 127.0.0.1:9000; fastcgi_pass unix:/var/run/php5-fpm.hamed.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } } error_page 404 /404.html; error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } }