1. Installation
1) Download the latest version of Nginx (currently version 1.5.13) from the Nginx official website download page (http://nginx.org/en/download.html) to install the package;
2) Copy to the deployment directory after decompression.
2. start and stop Nginx
Nginx currently only supports command line operations, before operation into the DOS command environment, and into the Nginx deployment directory.
1) Start Nginx:start nginx
2) Stop the Nginx:nginx-s stop
3) Reboot after configuration modification: Nginx-s Reload
These three commands can be made into bat files, placed in the deployment directory for easy follow-up operation.
Start Nginx.bat file contents: Start Nginx
Stop Nginx.bat file contents: Nginx-s stop
Reload Nginx.bat file contents: Nginx-s Reload
3. Reverse proxy configuration
Modify the contents of the nginx.conf file (such as nginx-1.5.13\conf\nginx.conf) in the Conf subdirectory under the deployment directory to adjust the relevant configuration.
Example of a reverse proxy configuration:
Location/{
#设置主机头和客户端真实地址 so that the server obtains the client's real IP
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_buffering off;
#设置反向代理的地址
Proxy_pass http://192.168.1.1;
}
The proxy address is modified according to the actual situation.
4. Load Balancing configuration
Nginx upstream by default is a poll-based load balancing, in this way, each request in chronological order to a different back-end server, if the backend server down, can be automatically rejected.
Another way is Ip_hash: Each request is allocated according to the hash result of the access IP, so that each visitor fixed access to a back-end server can solve the session problem.
Example of a load balancer configuration:
Upstream Backend {
#ip_hash;
Server 192.168.1.251;
Server 192.168.1.252;
Server 192.168.1.247;
}
server {
Listen 80;
server_name trffweb;
Location/{
#反向代理的地址
Proxy_pass Http://backend;
}
}
Upstream naming and server addresses are modified according to the actual situation.
5. Full Configuration Example
Nginx.conf:
Worker_processes 1;
Events {
Worker_connections 1024;
}
HTTP {
Include Mime.types;
Default_type Application/octet-stream;
Sendfile on;
Keepalive_timeout 65;
Upstream Backend {
#ip_hash;
Server 192.168.1.251;
Server 192.168.1.252;
Server 192.168.1.247;
}
server {
Listen 80;
server_name 2;
Location/{
#设置主机头和客户端真实地址 so that the server obtains the client's real IP
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_buffering off;
#反向代理的地址
Proxy_pass Http://backend;
}
}
}
Nginx reverse proxy and Load Balancer Deployment Guide