First prepare a few virtual machines and write down the corresponding configuration
192.168.1.54
————————>ubuntu11.4+nginx0.8.54 +php5.3.5
+msyql5.1 (load balancing server, php and mysql are not used here)
192.168.1.53 ————————>centos5.5+
apache+php5.3.5+mysql5.1 (web server)
192.168.1.55 ————————>centos5.5+
apache+php5.3.5+mysql5.1 (web server)
1. Simply access different servers according to the request suffix name, modify /etc/nginx/site_available/default
It is required that all visits to *.php point to 1.55, and all visits to *.html point to 1.53
server {
listen 80;
index index.php index.html index.htm;
server_namewww.test.com;
# key configuration
location ~ \.php$ {
proxy_pass http://192.168.1.55;
location ~ \.html$ {
proxy_pass http://192.168.1.53;
}
2. Simple access to different servers by domain name, it seems to be called reverse proxy
The nginx configuration of this version is scattered in several files, among which /etc/nginx/nginx.conf is configured as a global configuration, and the /etc/nginx/site_available/default file contains the configuration information of each virtual server, and then /etc /nginx/nginx.conf is included in /etc/nginx/site_available/default
Here we have two domain names img.test.com and www.test.com, both point to 192.168.1.54, all visits go to 192.168.1.54
, then 192.168.1.54
Then access different back-end servers according to different domain names, all visits to img.test.com are directed to 192.168.1.53, and all visits to www.test.com are directed to 192.168.1.55
Approach:
Configure /etc/nginx/nginx.conf first
Add the following settings,
upstream img.test.com{
server
192.168.1.53:80;
}
upstream www.test.com{
server
192.168.1.55:80;
}
Then modify /etc/nginx/site_available/default, configure two sever
server {
listen 80;
index index.php index.html index.htm;
server_name jssns.jse.edu.cn;
location / {
Proxy_pass
http://jssns.jse.edu.cn;
proxy_set_header
Host $host;
proxy_set_header
X-Real-IP $remote_addr;
proxy_set_header
X-Forwarded-For
$proxy_add_x_forwarded_for;
}
}server {
listen 80;
index index.php index.html
index.htm; server_name
img.jse.edu.cn; location / {
Proxy_pass
http://img.jse.edu.cn;
proxy_set_header
Host $host;
proxy_set_header
X-Real-IP $remote_addr;
proxy_set_header
X-Forwarded-For
$proxy_add_x_forwarded_for;
}
}
3. Multi-server simple load balancing
On the basis of the second, modify /etc/nginx/nginx.conf directly. Suppose we have two servers 1.56: 8080, 1.57, and 55. Two web servers are opened, using ports 80 and 8081 respectively
upstream img.test.com{
server
192.168.1.53:80;
server 192.168.1.56:8080;
}
upstream www.test.com{
server
192.168.1.55:80;
server
192.168.1.55:8081;
server
192.168.1.57:80;
}
nginx will balance everything to img.test.com to 192.168.1.53:80 and 192.168.1.56:8080;
All www.test.com requests are balanced to 192.168.1.55:80; 192.168.1.55:8081;
192.168.1.57:80;
4. Multi-server synchronous load balancing (session replication)
To be added. . .