Nginx和不同types的别名

我在我的Web服务器上有以下文件:

/var/www/html/--+ | +-misc--+ | | | +-misc1--+ | | | | | +-index.html | | | +-misc2--+ | | | | | +-index.php | | | +-misc3.php | +-wordpress--+ | +-index.php 

我有Nginx的设置,使http://example.com/进入我的WordPress的安装。 在我以前的(Apache)设置中,我能够轻松地创build别名来指向misc的项目,但我不确定如何在Nginx中执行此操作。

  index index.php index.html; root /var/www/html/wordpress; location ~ [^/]\.php(/|$) { limit_except GET POST {} fastcgi_split_path_info ^(.+?\.php)(/.*)$; if (!-f $document_root$fastcgi_script_name) { return 404; } fastcgi_buffer_size 16k; fastcgi_buffers 16 16k; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param PATH_INFO $fastcgi_path_info; fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info; fastcgi_param SERVER_NAME $host; fastcgi_param HTTP_PROXY ""; fastcgi_pass unix:/var/run/php-fpm.sock; fastcgi_index index.php; include fastcgi_params; } location ~* \.(?:css|js|jpg|jpeg|gif|png|mp4)$ { expires 1M; access_log off; add_header Cache-Control "public"; } location / { try_files $uri $uri/ /index.php; limit_except GET {} } 

我想要的是:

  • http://example.com/加载/var/www/html/wordpress/index.php
  • http://example.com/misc1加载/var/www/html/misc/misc1/index.html
  • http://example.com/misc2加载/var/www/html/misc/misc2/index.php
  • http://example.com/misc3.php加载/var/www/html/misc/misc3.php

我试过了:

 # http://example.com/misc1 shows index.html but anything else in the folder is 404 location /misc1 { alias /var/www/html/misc/misc1; } # http://example.com/misc1 gives 403, http://example.com/misc1/index.html gives 404 location /misc1/ { alias /var/www/html/misc/misc1; } # shows WordPress 404 page location /misc1/* { alias /var/www/html/misc/misc1; } # gives 403 error location ~ /misc1 { alias /var/www/html/misc/misc1; } 

我已经尝试过没有任何影响PHP,它不起作用。

不知道你为什么使用别名。 试试这个,它没有经过testing,但它应该让你至less接近你想要达到的目标。 如果它不工作评论为什么它不起作用的细节,并显示适用的日志和卷发。

 root /var/www/html/misc try_files $uri $uri/; # NB This can be specified at the server or location level location / { root /var/www/html/wordpress; try_files $uri $uri/ /index.php?$args; } location /misc1/ { root /var/www/html/misc; } location /misc2/ { root /var/www/html/misc; } 

编辑根据注释“只要我改变在服务器范围的根指令,我得到了我的Wordpress网站404,就好像它忽略了位置范围的根指令。 你可以试试这个。 当我在Wordpress的根目录下有一个自定义的PHP应用程序时,我使用这种技术。

 root /var/www/html/; location / { try_files $uri $uri/ /wordpress/index.php?$args; } 

事实certificate ,基于正则expression式的location块总是会覆盖其他location块。 我的configuration有两个正则expression式位置:该块意味着在静态资源上启用caching,而该块意味着启用PHP。 由于其中一个块匹配所有正在服务的内容,因此每个其他位置块都被忽略!

我最终做的是嵌套位置块 ,将静态文件和PHP块放在位置块内。 现在我的configuration如下所示:

php.inc

 location ~ [^/]\.php(/|$) { limit_except GET POST {} fastcgi_split_path_info ^(.+?\.php)(/.*)$; if (!-f $document_root$fastcgi_script_name) { return 404; } fastcgi_buffer_size 16k; fastcgi_buffers 16 16k; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param PATH_INFO $fastcgi_path_info; fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info; fastcgi_param SERVER_NAME $host; fastcgi_param HTTP_PROXY ""; fastcgi_pass unix:/var/run/php-fpm.sock; fastcgi_index index.php; include fastcgi_params; } 

static.inc

 location ~* \.(?:css|js|jpg|jpeg|gif|png|mp4)$ { expires 1M; access_log off; add_header Cache-Control "public"; } 

example.conf

 index index.php index.html; root /var/www/html/wordpress; location / { try_files $uri $uri/ /index.php; limit_except GET {} include conf.d/php.inc; include conf.d/static.inc; } location /misc1 { root /var/www/html/misc/; include conf.d/static.inc; } location /misc2 { root /var/www/html/misc/; include conf.d/php.inc; include conf.d/static.inc; } location /misc3.php { root /var/www/html/misc/; include conf.d/php.inc; }