PHP 的 Nginx 子目录根目录
Nginx subdirectory root with PHP
我在 docker 容器中 运行 nginx。我想要一个子目录 /web/
来访问我的个人文件和项目。它还应该支持 php.
下面是我的 运行 但 domain-a.com/web
一直导致 404。 PHP 已确认工作,因为相同的 php 块在子域上工作但是直接在 server{}
块中。
http {
server {
listen 443 ssl;
server_name domain-a.com domain-b.com;
# Mime types
include /etc/nginx/confs/mime.types;
# SSL
include /etc/nginx/confs/nginx-ssl.conf;
# Proxy to organizr
# This works
location / {
proxy_pass http://organizr/;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
# HTTP 1.1 support
proxy_http_version 1.1;
proxy_set_header Connection "";
}
# Root folder for my personal files/projects
# Doesn't work
location /web {
index index.php index.html;
root /etc/nginx/www;
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass php:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
}
}
}
如果您的文件在 /etc/nginx/www
中,您将需要使用 alias
指令,而不是 root
指令。有关详细信息,请参阅 this document。
例如:
location ^~ /web {
index index.php index.html;
alias /etc/nginx/www;
if (!-e $request_filename) { rewrite ^ /web/index.php last; }
location ~ \.php$ {
if (!-f $request_filename) { return 404; }
fastcgi_pass php:9000;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $request_filename;
}
}
使用$request_filename
获取别名文件的正确路径。避免 try_files
和 alias
因为 this issue. See this caution 使用 if
.
我在 docker 容器中 运行 nginx。我想要一个子目录 /web/
来访问我的个人文件和项目。它还应该支持 php.
下面是我的 运行 但 domain-a.com/web
一直导致 404。 PHP 已确认工作,因为相同的 php 块在子域上工作但是直接在 server{}
块中。
http {
server {
listen 443 ssl;
server_name domain-a.com domain-b.com;
# Mime types
include /etc/nginx/confs/mime.types;
# SSL
include /etc/nginx/confs/nginx-ssl.conf;
# Proxy to organizr
# This works
location / {
proxy_pass http://organizr/;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
# HTTP 1.1 support
proxy_http_version 1.1;
proxy_set_header Connection "";
}
# Root folder for my personal files/projects
# Doesn't work
location /web {
index index.php index.html;
root /etc/nginx/www;
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass php:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
}
}
}
如果您的文件在 /etc/nginx/www
中,您将需要使用 alias
指令,而不是 root
指令。有关详细信息,请参阅 this document。
例如:
location ^~ /web {
index index.php index.html;
alias /etc/nginx/www;
if (!-e $request_filename) { rewrite ^ /web/index.php last; }
location ~ \.php$ {
if (!-f $request_filename) { return 404; }
fastcgi_pass php:9000;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $request_filename;
}
}
使用$request_filename
获取别名文件的正确路径。避免 try_files
和 alias
因为 this issue. See this caution 使用 if
.