Nginx 配置疑问

272 天前
 yuanyuandeqiu
我配置了如下的代码,然后经常会出现第一天可以使用,但是到了第二天就无法使用的情况,解决办法就是在后面加上'/',或者去掉'/',这样就能正常使用了,请问这是什么原因

server {
listen 82;
location / {
proxy_pass https://example.com;

}

location /api {
proxy_pass http://127.0.0.1:8848;
# proxy_pass http://127.0.0.1:8848/;
}
}
1478 次点击
所在节点    NGINX
16 条回复
CnpPt
272 天前
按照你的说法,问题不在/上,而且你可能不清楚加不加/的区别,第二天是哪个 location 无法使用?如果是第一个 location /,proxy_pass 反代的域名是不是 DDNS 域名 经常变 IP ?
yuanyuandeqiu
272 天前
@CnpPt 加不加/的区别有什么区别吗,这个我不知道

然后这个是两个都会出现无法使用的情况,然后改成另一个就可以了,这期间域名也没有改变
maclanelf134
272 天前
@yuanyuandeqiu 有区别,可以去查查,不清楚你那边的具体是啥业务场景,可以在提供细一点,是反代域名有问题,还是反代下面那个 ip 出问题
yulgang
272 天前
后端服务器处理路径时 /api 和 /api/ 这两种情况有不同的行为?隔天不好用感觉像是浏览器缓存的问题
skiy
272 天前
你看看 8848 端口的日志,看请求的 URI 是什么,对比一下。
yuanyuandeqiu
272 天前
@skiy 看日志的话 URI 是一样的
@yulgang 之前的话是两个都不行,报 404 ,然后我开了个无痕模式就可以了,再然后就出现两个地址轮流出问题的情况,不知道和缓存有没有关系
@maclanelf134 其他人的都没问题,应该是我的问题,一样的配置就我的会这样,不知道和 nginx 版本有没有关系
yulgang
272 天前
@yuanyuandeqiu #6

nginx -T

贴出来让大家帮你看看
yuanyuandeqiu
272 天前
nginx: the configuration file D:\programe\nginx-1.24.0/conf/nginx.conf syntax is ok
nginx: configuration file D:\programe\nginx-1.24.0/conf/nginx.conf test is successful
# configuration file D:\programe\nginx-1.24.0/conf/nginx.conf:

worker_processes 1;

events {
worker_connections 1024;
}


http {
include mime.types;
default_type application/octet-stream;


sendfile on;
tcp_nopush on;
tcp_nodelay on;

keepalive_timeout 65;

gzip on;
gzip_disable "msie6";

server {
listen 80;
server_name localhost;

location /scoreserv {
proxy_pass http://127.0.0.1:8080;
}
location / {
proxy_pass https://example1.com/;
}

error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}

server {
listen 82;
location / {
proxy_pass https://example2.com;

}

location /api {
proxy_pass http://127.0.0.1:8848;
}

}

}
yuanyuandeqiu
272 天前
# configuration file D:\programe\nginx-1.24.0/conf/mime.types:

types {
text/html html htm shtml;
text/css css;
text/xml xml;
image/gif gif;
image/jpeg jpeg jpg;
application/javascript js;
application/atom+xml atom;
application/rss+xml rss;

text/mathml mml;
text/plain txt;
text/vnd.sun.j2me.app-descriptor jad;
text/vnd.wap.wml wml;
text/x-component htc;

image/avif avif;
image/png png;
image/svg+xml svg svgz;
image/tiff tif tiff;
image/vnd.wap.wbmp wbmp;
image/webp webp;
image/x-icon ico;
image/x-jng jng;
image/x-ms-bmp bmp;

font/woff woff;
font/woff2 woff2;

application/java-archive jar war ear;
application/json json;
application/mac-binhex40 hqx;
application/msword doc;
application/pdf pdf;
application/postscript ps eps ai;
application/rtf rtf;
application/vnd.apple.mpegurl m3u8;
application/vnd.google-earth.kml+xml kml;
application/vnd.google-earth.kmz kmz;
application/vnd.ms-excel xls;
application/vnd.ms-fontobject eot;
application/vnd.ms-powerpoint ppt;
application/vnd.oasis.opendocument.graphics odg;
application/vnd.oasis.opendocument.presentation odp;
application/vnd.oasis.opendocument.spreadsheet ods;
application/vnd.oasis.opendocument.text odt;
application/vnd.openxmlformats-officedocument.presentationml.presentation
pptx;
application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
xlsx;
application/vnd.openxmlformats-officedocument.wordprocessingml.document
docx;
application/vnd.wap.wmlc wmlc;
application/wasm wasm;
application/x-7z-compressed 7z;
application/x-cocoa cco;
application/x-java-archive-diff jardiff;
application/x-java-jnlp-file jnlp;
application/x-makeself run;
application/x-perl pl pm;
application/x-pilot prc pdb;
application/x-rar-compressed rar;
application/x-redhat-package-manager rpm;
application/x-sea sea;
application/x-shockwave-flash swf;
application/x-stuffit sit;
application/x-tcl tcl tk;
application/x-x509-ca-cert der pem crt;
application/x-xpinstall xpi;
application/xhtml+xml xhtml;
application/xspf+xml xspf;
application/zip zip;

application/octet-stream bin exe dll;
application/octet-stream deb;
application/octet-stream dmg;
application/octet-stream iso img;
application/octet-stream msi msp msm;

audio/midi mid midi kar;
audio/mpeg mp3;
audio/ogg ogg;
audio/x-m4a m4a;
audio/x-realaudio ra;

video/3gpp 3gpp 3gp;
video/mp2t ts;
video/mp4 mp4;
video/mpeg mpeg mpg;
video/quicktime mov;
video/webm webm;
video/x-flv flv;
video/x-m4v m4v;
video/x-mng mng;
video/x-ms-asf asx asf;
video/x-ms-wmv wmv;
video/x-msvideo avi;
}
yuanyuandeqiu
272 天前
@yulgang nginx -T 感觉没什么问题
julyclyde
272 天前
无论加还是不加,都不可能出现“第二天就不能用”的情况
应该是从头到尾行为一致的
my3157
272 天前
域名最好加个 resolver
mmdsun
272 天前
location /api/和 location /api 、proxy_pass 结尾加不加/有区别的。会决定是否吧请求的路径中的/api 转发到后端 proxy_pass 。地址。

你这可能有缓存吧?或者多个 nginx 进程没杀死,好像以前 Windows nginx 会有这个问题,taskkill /f /t 批量结束下
yulgang
269 天前
@yuanyuandeqiu #10

不知道你有没有解决
试一试禁用缓存看看:

location /api {
proxy_pass http://127.0.0.1:8848;
proxy_cache_bypass $http_cache_control;
add_header Cache-Control 'no-cache';
}

或者

location /api {
proxy_pass http://127.0.0.1:8848;
proxy_cache_valid 200 302 10m; # 设置 200 和 302 响应的缓存时间为 10 分钟
proxy_cache_bypass $http_cache_control;
add_header Cache-Control 'no-cache';
}
gotosre
267 天前
带不带斜杠也就是做不做 prefix trim 的区别,,,至于玄学问题么,大概率是变配错觉
yuanyuandeqiu
266 天前
@yulgang 已经解决了,现在可以用固定的配置访问了

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/964497

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX