Nginx 反向代理,流量转发到固定内网 IP 方法

 主配置文件:

user  nginx;
worker_processes  1;
error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;
events {
    worker_connections  1024;
}
http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
    access_log  /var/log/nginx/access.log  main;
    sendfile        on;
    #tcp_nopush     on;
    keepalive_timeout  65;
    gzip  on;
#################
 proxy_connect_timeout 300;
    proxy_send_timeout 300;
    proxy_read_timeout 300;
    proxy_buffer_size 16k;
    proxy_buffers 4 32k;
 
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    #proxy_set_header X-Forwarded-For $remote_addr;
    proxy_set_header Connection Close;
    server_names_hash_max_size 1024;
    server_names_hash_bucket_size 1024;
    # Default cache parameters for use by virtual hosts
    # Set the cache path to tmpfs mounted disk, and the zone name
    # Set the maximum size of the on disk cache to less than the tmpfs file system size
    proxy_cache_path  ./cache  levels=1:2  keys_zone=pscms:100m max_size=800m;
    proxy_temp_path   ./proxy;
#############
    include /etc/nginx/conf.d/*.conf;
}

网站配置文件:

upstream web_server {
        #sticky;
        server 192.168.203.23;
        server 192.168.203.24;
        server 192.168.203.25;
     #  ip_hash;
    }
server {
    listen       80;
    server_name  localhost;
    charset utf-8;
    access_log  /var/log/nginx/21.log  main;
location / {
if ( $remote_addr ~* ^(.*)\.(.*)\.(.*)\.193$){
                proxy_pass http://192.168.203.22;
                break;
        }
            proxy_set_header Host $http_host;
            proxy_redirect     off;
            proxy_pass         http://web_server;
            proxy_set_header   Host             $host;
            proxy_set_header   X-Real-IP        $remote_addr;
            proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
        }
}

本文 暂无 评论

回复给

Top