目的,将对本机80端口的访问代理到192.168.121.11
原配置:
server {
listen 80;
server_name localhost;
#charset koi8-r;
access_log logs/host.access.log main;
location = /index.html {
root /mnt/app/Web;
index index.html index.htm;
add_header Cache-Control no-cache;
}
location ~* ^.+\/[^\/]+(?=\.)([^\/](?!(ini)))+$ {
root /mnt/app/Web;
index index.html index.htm;
add_header Cache-Control no-cache;
}
location / {
root /mnt/app/Web/;
index index.html index.htm;
}
#代理
location /head {
#proxy_pass http://100.100.100.102/;
proxy_pass http://192.168.121.11/;
}
}
但是发现这么做无法加载静态资源(js、css等),尝试新加端口代理,可以正常加载:
server {
listen 8000;
server_name head;
location /head {
#proxy_pass http://100.100.100.102/;
proxy_pass http://172.18.80.94/;
}
}
但由于业务需要,必须使用80端口。
找了很久找不到原因,后续终于在某篇文章找到了类似问题,改为如下配置后成功代理:
server {
listen 80;
server_name localhost;
#charset koi8-r;
access_log logs/host.access.log main;
location = /index.html {
root /mnt/app/Web;
index index.html index.htm;
add_header Cache-Control no-cache;
}
location ~* ^.+\/[^\/]+(?=\.)([^\/](?!(ini)))+$ {
root /mnt/app/Web;
index index.html index.htm;
add_header Cache-Control no-cache;
}
location / {
root /mnt/app/Web/;
index index.html index.htm;
}
#增加“^~”后生效
location ^~ /head {
#proxy_pass http://100.100.100.102/;
proxy_pass http://192.168.121.11/;
}
}
不太能理解为什么8000端口直接就能生效的80端口就要加个^~才可以。。。