小白刚入手nginx,望大n....
我在本地添加了:seren.proxy.cn、seren.html.cn 两个站点。
虽然代理能访问了,但访问“seren.html.cn”下的php文件却无法访问,奇怪 html能行。
像单纯写“seren.proxy.cn/api”是能访问index.php, 在“seren.proxy.cn/api”后面加上“index.php”又不行了真磨人啊
seren.proxy.cn 配置
server {
listen 80;
server_name www.seren.proxy.cn seren.proxy.cn;
root "F:/seren/www/proxy";
location / {
index index.php index.html;
autoindex off;
}
location /api/{
rewrite ^/api/(.*)$ /$1 break;
proxy_pass http://seren.html.cn;
}
location ~ .*\.(js|css|jpg|jpeg|gif|png|ico|pdf|txt)$ {
proxy_pass http://seren.html.cn;
}
location ~ \.php(.*)$ {
fastcgi_pass 127.0.0.1:9001;
fastcgi_index index.php;
fastcgi_split_path_info ^((?U).+\.php)(/?.+)$;
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;
include fastcgi_params;
}
}
seren.html.cn 配置
server {
listen 80;
server_name seren.html.cn;
root "F:/seren/www/proxyhtml";
location / {
index index.php index.html;
autoindex off;
}
location ~ \.php(.*)$ {
fastcgi_pass 127.0.0.1:9001;
fastcgi_index index.php;
fastcgi_split_path_info ^((?U).+\.php)(/?.+)$;
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;
include fastcgi_params;
}
}
你这个完全就是 location匹配的顺序问题
你使用的是
普通匹配
, 普通匹配成功了不会停止匹配的, 如果同时也有正则匹配
并且成功匹配到了, 那么就会优先使用正则匹配的结果明显的, 你的url
http://seren.proxy.cn/api/index.php
先匹配到了location /api/
,但是因为这个规则只是普通匹配,所以又继续往下匹配, 这时又一个规则
location ~ \.php(.*)$
被匹配上了, 并且是正则匹配, 优先级比普通匹配要高所以最终使用是这个规则, 自然也是这个规则来处理你的url
你一开始使用的url是
http://seren.proxy.cn/api
, 因为这个只满足一个匹配结果, 所以没有问题解决方法很简单
加上
^~
, 让它变成精准的前缀匹配
就可以了精准前缀匹配是匹配到之后立即结束, 不往下继续匹配
你可以看看我的文章: https://www.xstnet.com/articl...