普通七层代理 在http层添加配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
server { listen 8080; listen [::]:8080; server_name _; root /usr/share/nginx/html; include /etc/nginx/default.d/*.conf; error_page 404 /404.html; location = /404.html {} error_page 500 502 503 504 /50x.html; location = /50x.html {} location / { proxy_pass http://10.12.1.8:8080; proxy_set_header Host $host:$server_port; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } } server { listen 8081 ssl; listen [::]:8081 ssl; server_name _; root /usr/share/nginx/html; ssl_certificate "/etc/nginx/ca.crt"; ssl_certificate_key "/etc/nginx/ca.key"; include /etc/nginx/default.d/*.conf; error_page 404 /404.html; location = /404.html {} error_page 500 502 503 504 /50x.html; location = /50x.html {} location / { proxy_pass https://10.12.1.8:8081; proxy_set_header Host $host:$server_port; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } |
四层代理: 四层代理需要stream模块,默认可能没有安装,如果是centos系列,可以直接yum将所有模块都安装上 yum install nginx-all-modules 在nginx.conf最上层添加配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
stream { upstream http { server 10.12.1.8:8080; } upstream https { server 10.12.1.8:8081; } server { listen 8080; proxy_connect_timeout 3s; proxy_timeout 3s; proxy_pass http; } server { listen 8081; proxy_connect_timeout 3s; proxy_timeout 3s; proxy_pass https; } } |