当后端web服务器存在多个虚拟主机时,该模块是用来区分前端请求,是给反向代理的后端的哪个虚拟主机 1 在服务器192.168.132.131 前端服务器如下配置: upstream backend { server 192.168.132.129 max_fails=3 fail_timeout=30s; server 192.168.132.130 max_fails=3 fail_timeout=30s; } server { listen 80; server_name www.etiantian.org; index index.php index.html index.htm; location / { proxy_pass http://backend; } } server { listen 80; server_name bbs.etiantian.org; root html/bbs; index index.html index.htm; location / { proxy_pass http://backend; } } } [root@moban conf]# cat /etc/hosts 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 192.168.132.131 www.etiantian.org bbs.etiantian.org blog.etiantian.org status.etiantian.org [root@moban conf]# [root@moban conf]# for n in `seq 100`; do curl bbs.etiantian.org;sleep 2; done bbs.etiantian.org www.etiantian.org bbs.etiantian.org www.etiantian.org bbs.etiantian.org www.etiantian.org bbs.etiantian.org 从这可以看出你访问到后端时,服务器并不知到你想访问后端的哪个虚拟主机,所以他就将第一个虚拟主机返回给你,显然这是不正确的!!!! 那么解决这个方法的途径就是在 location 标签中加上 proxy_set_header Host $host; 后端服务器就知道你想访问的是哪个虚拟主机了。 server { listen 80; server_name bbs.etiantian.org; root html/bbs; index index.html index.htm; location / { proxy_pass http://backend; proxy_set_header Host $host; } } } [root@moban conf]# for n in `seq 100`; do curl bbs.etiantian.org;sleep 2; done http://bbs.etinatian.org bbs.etiantian.org http://bbs.etinatian.org bbs.etiantian.org http://bbs.etinatian.org bbs.etiantian.org http://bbs.etinatian.org bbs.etiantian.org 2 在后端服务器192.168.132.129 这样配置: [root@moban conf]# cat nginx.conf worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; server { listen 80; server_name www.etiantian.org; root html/www; index index.php index.html index.htm; } server { listen 80; server_name bbs.etiantian.org; root html/bbs; index index.html index.htm; } } [root@moban conf]# curl www.etiantian.org www.etiantian.org [root@moban conf]# curl bbs.etiantian.org http://bbs.etinatian.org [root@moban conf]# 3 在另一后端服务器192.168.132.130如下配置: [root@moban conf]# cat nginx.conf worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; server { listen 80; server_name bbs.etiantian.org; root html/bbs; index index.html index.htm; } server { listen 80; server_name www.etiantian.org; root html/www; index index.php index.html index.htm; } } [root@moban conf]# curl bbs.etiantian.org bbs.etiantian.org [root@moban conf]# curl www.etiantian.org http://www.etiantian.org [root@moban conf]#
11,nginx proxy_set_header Host $host 模块讲解
原文:https://www.cnblogs.com/k8s-pod/p/13911942.html