nginx反向代理配置

系统环境

DMZ区有一台前置机器A,后端一台服务器为应用系统B,另外一台为数据库服务器C。
准备在A上配置反向代理服务器B。php

配置状况

在A上配置nginx的反向代理,最初调整配置文件以下:css

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/。因此此处配置时增长了"/"html

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”,因此考虑修改如下内容:nginx

location ~ .*\.(js|css)$ {
    proxy_pass    http://10.23.65.131:8080/js1/;
}

但修改后发现nginx启动报错,提示使用正则表达式时候不能使用URI的请求方式。因此按照静态资源部署方式,在A服务器上部署静态资源,路径与B上保持了一致,以下所示:es6

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,可是没有尝试成功,后续待测试tomcat

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;
相关文章
相关标签/搜索