在过去的几天里,我一直在与此斗争,我希望有更深层次的理解的人能为我指明正确的方向。
我需要使用PHP从现有PHP应用程序的子目录中“根”提供辅助PHP应用程序。
结构是
- /var/www/www.example.com/
- public/
- index.php
- subapplication/
- public/
- index.php这两个应用程序都可以从www.example.com和www.example.com/subapplication/的相同域中获得。
我可以直接使用nginx服务这些应用程序中的任何一个,但是我很难让我的指令正常工作,这样我就可以正确地为主应用程序和子应用程序服务。
这是我在这一点上最接近的配置,但仍然不太好用。
server {
listen 80 default_server;
server_name www.example.com;
set $base /var/www/www.example.com;
index index.php;
location /subapplication/ {
alias $base/subapplication/public;
try_files $uri /subapplication/index.php?$query_string;
location ~ \.php$ {
# 404
try_files $fastcgi_script_name =404;
# default fastcgi_params
include fastcgi_params;
# fastcgi settings
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
fastcgi_index index.php;
fastcgi_buffers 8 16k;
fastcgi_buffer_size 32k;
# fastcgi params
fastcgi_param DOCUMENT_ROOT $realpath_root;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
}
}
location / {
root $base/public;
try_files $uri $uri/ /index.php?$query_string;
location ~ \.php$ {
# 404
try_files $fastcgi_script_name =404;
# default fastcgi_params
include fastcgi_params;
# fastcgi settings
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
fastcgi_index index.php;
fastcgi_buffers 8 16k;
fastcgi_buffer_size 32k;
# fastcgi params
fastcgi_param DOCUMENT_ROOT $realpath_root;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
}
}
}我漏掉了什么明显的东西吗?
目前,我只需为所有对/subapplication/路由的请求获得一个D5。将$uri/添加到location /subapplication/下的try_files会给出一个403,说明它不能索引subapplication/public文件夹。
我会非常感激任何人所能提供的任何见解。
发布于 2020-04-01 19:23:55
作为以后遇到这个问题的人的一个标杆,alias和try_files有一个长期存在的问题,一直困扰着我。
解决方案可以在理查德·史密斯的回答中找到
我的工作配置:
# www.example.com configuration #
server {
listen 80 default_server;
server_name www.example.com;
set $base /var/www/www.example.com;
root $base/public;
index index.php;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
# @SEE https://serverfault.com/a/828579/212904 #
location ^~ /subapplication {
alias $base/subapplication/public;
if (!-e $request_filename) {
rewrite ^ /subapplication/index.php last;
}
location ~ \.php$ {
if (!-f $request_filename) {
rewrite ^ /subapplication/index.php last;
}
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
fastcgi_param SCRIPT_FILENAME $request_filename;
}
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
}
}发布于 2020-04-01 11:25:08
尝试以下几点
server {
listen 80;
root /var/www/www.example.com;
index index.php index.html index.htm index.php index.nginx-debian.html;
server_name www.example.com;
location ~* ^.+\.(js|css|ogg|ogv|svg|svgz|eot|otf|woff|mp4|ttf|rss|atom|jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|tar|mid|midi|wav|bmp|rtf)$ {
access_log off;
add_header Access-Control-Allow-Origin *;
expires 30d;
}
location / {
try_files $uri $uri/ /index.php?q=$uri&$args;
}
location = /xmlrpc.php {
return 404;
}
location /subapplication {
try_files $uri $uri/subapplication /subapplication/index.php?q=$uri&$args;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}
location /. {
return 404;
}
}https://serverfault.com/questions/1010321
复制相似问题