我的网络服务器上有以下文件:
/var/www/html/--+
|
+-misc--+
| |
| +-misc1--+
| | |
| | +-index.html
| |
| +-misc2--+
| | |
| | +-index.php
| |
| +-misc3.php
|
+-wordpress--+
|
+-index.php
我已经设置了 Nginx,以便http://example.com/转到我的 Wordpress 安装。在我之前的 (Apache) 设置中,我能够轻松创建别名来指向其中的项目,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 没有任何影响,它不起作用。
答案1
不确定你为什么要使用别名。试试这个,它没有经过测试,但它应该至少能让你更接近你想要实现的目标。如果它不起作用,请详细说明它不起作用的原因,并显示适用的日志和卷曲。
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,就好像它忽略了位置范围内的根指令一样。”您可以尝试此方法。当我在根目录中有一个自定义 PHP 应用程序,而 Wordpress 在子目录中时,我会使用此技术。
root /var/www/html/;
location / {
try_files $uri $uri/ /wordpress/index.php?$args;
}
答案2
所以事实证明,location
基于正则表达式的块将始终覆盖其他location
块。我的配置有两个正则表达式位置:用于启用静态资源缓存的块和用于启用 PHP 的块。由于其中一个块与正在提供的所有内容相匹配,其他所有位置块均被忽略!
我最终做的是嵌套位置块,将静态文件和 PHP 块放在位置块内。现在我的配置如下所示:
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;
}
静态公司
location ~* \.(?:css|js|jpg|jpeg|gif|png|mp4)$ {
expires 1M;
access_log off;
add_header Cache-Control "public";
}
例子.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;
}