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"';

    sendfile 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 Connection Close;

    server_names_hash_max_size 1024;
    server_names_hash_bucket_size 1024;

    proxy_cache_path ./proxy_cache levels=1:2 keys_zone=cache_cache:100m max_size=1024m;

# proxy_temp_path ./proxy;

#############

    include /etc/nginx/conf.d/*.conf;
}

 


网站配置文件:

upstream web {
    server 192.168.2.11;
    server 192.168.3.22;
    server 192.168.4.33;
    # ip_hash;
}
server {
    listen 80;
    server_name localhost;
    charset utf-8;

    location / {
        if ( $remote_addr ~* ^(.*).(.*).(.*).111$){
        proxy_pass http://192.168.1.22;
        break;
    }

    proxy_set_header Host $http_host;
    proxy_redirect off;
    proxy_pass http://web;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}

posted @ 2022-06-27 15:36  来自牧羊人  阅读(1866)  评论(0编辑  收藏  举报