我为我们的产品(基于php的微服务)构建了一些容器,但有一个我无法理解的问题:
我有3个容器: -Nginx -php-fpm +代码 -php-fpm +代码
Nginx被配置为php-fpm的反向代理:
server {
listen 80;
#listen 443;
server_name 1.mydomain.com;
index index.php;
error_log /var/log/nginx/sso-error.log;
access_log /var/log/nginx/sso-access.log;
root /1;
location ~ [^/]\.php(/|$) {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
fastcgi_pass php-fpm1:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
add_header 'Access-Control-Allow-Credentials' 'true';
add_header 'Access-Control-Allow-Headers' 'Authorization, Keep-Alive, User-Agent, X-Requested-With, If-Modified-Since, Cache-Control, Content-Type';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Expose-Headers' 'X-Domain-Token';
add_header 'Access-Control-Max-Age' 86400;
}
}
server {
listen 80;
#listen 443;
server_name 2.mydomain.com;
index index.php;
error_log /var/log/nginx/ajax-broker.error.log;
access_log /var/log/nginx/ajax-broker.access.log;
root /2;
location ~ [^/]\.php(/|$) {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
fastcgi_pass php-fpm2:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
add_header 'Access-Control-Allow-Credentials' 'true';
add_header 'Access-Control-Allow-Headers' 'Authorization, Keep-Alive, User-Agent, X-Requested-With, If-Modified-Since, Cache-Control, Content-Type';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Expose-Headers' 'X-Domain-Token';
add_header 'Access-Control-Max-Age' 86400;
}
}
如果,我将带有代码的卷从php-fpm挂载到nginx容器-一切正常, 但是,如果我仅将代码存储在fpm容器中-nginx也会返回404,也适用于* php URI。
如何在不进行安装的情况下正确设置nginx。
答案 0 :(得分:0)
好吧,经过几次实验我找到了解决方案,您应该再添加一个位置并在php部分中注释 try_files :
server {
listen 80;
#listen 443;
server_name 1.mydomain.com;
index index.php;
error_log /var/log/nginx/sso-error.log;
access_log /var/log/nginx/sso-access.log;
root /1;
location / {
try_files $uri $uri/index.php;
}
location ~ [^/]\.php(/|$) {
#try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
fastcgi_pass php-fpm1:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
add_header 'Access-Control-Allow-Credentials' 'true';
add_header 'Access-Control-Allow-Headers' 'Authorization, Keep-Alive, User-Agent,
X-Requested-With, If-Modified-Since, Cache-Control, Content-Type';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Expose-Headers' 'X-Domain-Token';
add_header 'Access-Control-Max-Age' 86400;
}
}
server {
listen 80;
#listen 443;
server_name 2.mydomain.com;
index index.php;
error_log /var/log/nginx/ajax-broker.error.log;
access_log /var/log/nginx/ajax-broker.access.log;
root /2;
location / {
try_files $uri $uri/index.php;
}
location ~ [^/]\.php(/|$) {
#try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
fastcgi_pass php-fpm2:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
add_header 'Access-Control-Allow-Credentials' 'true';
add_header 'Access-Control-Allow-Headers' 'Authorization, Keep-Alive, User-Agent,
X-Requested-With, If-Modified-Since, Cache-Control, Content-Type';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Expose-Headers' 'X-Domain-Token';
add_header 'Access-Control-Max-Age' 86400;
}
}