nginx做負載+apache多虛擬web主機的部署1笨方法
阿新 • • 發佈:2019-01-01
apache1 apache2
vim /etc/httpd/conf.d/www.conf <VirtualHost *:81> DocumentRoot /var/www/html/www ServerName www.wkphp.com <Directory "/var/www/html/www"> Options Indexes FollowSymLinks AllowOverride All Order allow,deny Allow from all </Directory> </VirtualHost> <VirtualHost *:82> DocumentRoot /var/www/html/www ServerName www.wkhttp.com <Directory "/var/www/html/www"> Options Indexes FollowSymLinks AllowOverride All Order allow,deny Allow from all </Directory> </VirtualHost> <VirtualHost *:83> DocumentRoot /var/www/html/123 ServerName www123php.com <Directory "/var/www/html/123"> Options Indexes FollowSymLinks AllowOverride All Order allow,deny Allow from all </Directory> </VirtualHost> <VirtualHost *:84> DocumentRoot /var/www/html/123 ServerName www.123http.com <Directory "/var/www/html/123"> Options Indexes FollowSymLinks AllowOverride All Order allow,deny Allow from all </Directory> </VirtualHost> listen 81 listen 82 listen 83 listen 84
將web虛擬主機指定到不同的埠,再有nginx指定不同的域名來進行負載
vim nginx.conf 在http節點中新增
upstream www.wkphp.com { server 192.168.3.3:81 weight=1; server 192.168.3.4:81 weight=1; } server { listen 80; server_name www.wkphp.com; location / { proxy_pass http://www.wkphp.com/; index index.html index.htm index.php; 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; } } upstream www.wkhttp.com { server 192.168.3.3:82 weight=1; server 192.168.3.4:82 weight=1; } server { listen 80; server_name www.wkhttp.com; location / { proxy_pass http://www.wkhttp.com/; index index.html index.htm; 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; } } upstream www.123php.com { server 192.168.3.3:83 weight=1; server 192.168.3.4:83 weight=1; } server { listen 80; server_name www.123php.com; location / { proxy_pass http://www.123php.com/; index index.html index.htm; 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; } } upstream www.123http.com { server 192.168.3.3:84 weight=1; server 192.168.3.4:84 weight=1; } server { listen 80; server_name www.123http.com; location / { proxy_pass http://www.123http.com/; index index.html index.htm; 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; } }
重點解讀1 proxy_pass http://www.123php.com/; 此時的www.123php.com的名稱和servername保持一致,並且和upstream www.123http.com 的名稱要保持一致
重點解讀2 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;
帶上host header