子文件夹中的nginx项目

我对我的nginxconfiguration感到沮丧,所以我在写configuration文件时要求帮助,以便从同一根目录中的子目录提供多个项目。 这不是虚拟主机,因为它们都使用相同的主机值。 也许一个例子会澄清我的尝试:

  • 请求192.168.1.1/应该从/var/www/public/
  • 请求192.168.1.1/wiki/应该从/var/www/wiki/public/
  • 请求192.168.1.1/blog/应该从/var/www/blog/public/

这些项目正在使用PHP并使用fastcgi。

我目前的configuration是非常小的。

 server { listen 80 default; server_name localhost; access_log /var/log/nginx/localhost.access.log; root /var/www; index index.php index.html; location ~ \.php$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /var/www$fastcgi_script_name; include fastcgi_params; } } 

我已经尝试了aliasrewrite各种东西,但无法为fastcgi正确设置事物。 看起来应该比写位置块和复制rootindexSCRIPT_FILENAME等更有说服力的方法。

任何指引,让我朝着正确的方向是值得赞赏的。

由于您的项目实际上不在同一个根目录中,因此您必须为此使用多个位置。

 location /wiki { root /var/www/wiki/public; } location ~ /wiki/.+\.php$ { fastcgi_pass 127.0.0.1:9000; fastcgi_param SCRIPT_FILENAME /var/www/wiki/public$fastcgi_script_name; } location /blog { root /var/www/blog/public; } location ~ /blog/.+\.php$ { fastcgi_pass 127.0.0.1:9000; fastcgi_param SCRIPT_FILENAME /var/www/blog/public$fastcgi_script_name; } 

另外,将fastcgi_index放在你的fastcgi_params文件中,并将其包含在服务器级别,这样你的php位置尽可能小。

通过位置+别名解决:

location / { root /var/www/public; index index.php; } location /blog/ { alias /var/www/blog/public/; index index.php; } location /wiki/ { alias /var/www/wiki/public/; index index.php; } location ~ \.php$ { #your fastcgi configuration here }
location / { root /var/www/public; index index.php; } location /blog/ { alias /var/www/blog/public/; index index.php; } location /wiki/ { alias /var/www/wiki/public/; index index.php; } location ~ \.php$ { #your fastcgi configuration here } 

这是我尝试,更多详细信息在http://programmersjunk.blogspot.com/2013/11/nginx-multiple-sites-in-subdirectories.html

  location /Site1/ { root /usr/share/nginx/www/Site1; try_files $uri $uri/ /index.php?$query_string; } # the images need a seperate entry as we dont want to concatenate that with index.php location ~ /Site1/.+\.(jpg|jpeg|gif|css|png|js|ico|xml)$ { root /usr/share/nginx/www/Site1; } # pass the PHP scripts to FastCGI server location ~ /Site1/.+\.php$ { fastcgi_split_path_info ^(.+\.php)(/.+)$; allow 127.0.0.1; # # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini # # With php5-fpm: fastcgi_pass unix:/var/run/php5-fpm.sock; include fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_index index.php; } location /Site3/ { root /usr/share/nginx/www/Site3; } # pass the PHP scripts to FastCGI server location ~ /Site3/.+\.php$ { allow 127.0.0.1; fastcgi_pass unix:/var/run/php5-fpm.sock; include fastcgi_params; #we are directly using the $request_filename as its a single php script fastcgi_param SCRIPT_FILENAME $request_filename; }