系统环境
DMZ区有一台前置机器A,后端一台服务器为应用系统B,另一台为数据库服务器C。
准备在A上配置反向代理服务器B。
配置情况
在A上配置nginx的反向代理,最初调整配置文件如下:
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 8523;
server_name 10.23.65.111;
location / {
index index.php index.html index.htm;
proxy_pass http://10.23.65.131:8080/js1/;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size 10m;
client_body_buffer_size 128k;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
}
location ~ .*\.(js|css)$ {
proxy_pass http://10.23.65.131:8080;
}
add_header X-Via $server_addr;
add_header X_cache_hit $upstream_cache_status;
}
}
问题1
下面配置中的URI是否需要加"/"。按以往配置是不加的,但是后端B用了tomcat的虚拟路径,指定访问/js1后会自动变为/js1/。所以此处配置时增加了"/"
location / {
index index.php index.html index.htm;
proxy_pass http://10.23.65.131:8080/js1/;
}
问题2
加载页面时发现js/css静态文件的无法加载,请求路径变为了“http://10.23.65.111/js1/xxx.js”,所以考虑修改以下内容:
location ~ .*\.(js|css)$ {
proxy_pass http://10.23.65.131:8080/js1/;
}
但修改后发现nginx启动报错,提示使用正则表达式时候不能使用URI的请求方式。所以按照静态资源部署方式,在A服务器上部署静态资源,路径与B上保持了一致,如下所示:
location ~ .*\.(js|css)$ {
root E:\software\es6tutorial-gh-pages;
}
配置后访问正常
问题3
如果还想使用proxy_pass怎么办?于是尝试修改location,如下所示:
location /js/$ {
proxy_pass http://10.23.65.131:8080;
}
配置后访问正常
问题4
想尝试使用rewrite的方式配置js/css,但是没有尝试成功,后续待测试
location ~ .*\.(js|css)$ {
rewrite ???
proxy_pass http://10.23.65.131:8080;
}
问题4
举例说明:DMZ区服务器A的内网地址测试为10.23.65.111:8523,映射公网测试为IP:PortA。曾经出现过公网访问时IP:PortA变为了IP:8523的情况,本次测试却没出现,不知道具体是哪里配置除了问题,待观察。
PS:网上有说可以在配置变为如下形式解决此问题,下次出现需要测试。
proxy_set_header Host $host;
#修改为
proxy_set_header Host $host:$server_port;