Nginx ("engine x") is a high-performance HTTP and reverse proxy server, as well as an IMAP/POP3/SMTP server. As a reverse proxy server, Nginx can accelerate web servers and has load balancing functions. nginx can automatically eliminate servers that have stopped serving to ensure normal access to web services.
In this example, a user accesses 192.168.1.4 and proxies it to the two servers 192.168.1.2:80 and 192.168.1.3:80 to implement the load.
The following is the configuration file nginx.conf:
user www www; worker_processes 10; #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #pid logs/nginx.pid; #最大文件描述符 worker_rlimit_nofile 51200; events { worker_connections 51200; } http { include conf/mime.types; default_type application/octet-stream; keepalive_timeout 120; tcp_nodelay on; #nginx的upstream默认是以轮询的方式实现负载均衡,这种方式中,每个请求按时间顺序逐一分配到不同的后端服务器, #如果后端服务器down掉,能自动剔除。另外一种方式是ip_hash:每个请求按访问ip的hash结果分配, #这样每个访客固定访问一个后端服务器,可以解决session的问题。 upstream webserver { server 192.168.1.2:80 weight=1; server 192.168.1.3:80 weight=1; } server{ listen 80; server_name localhost; location / { #设置反向代理的地址 proxy_pass http://webserver; #设置主机头和客户端真实地址,以便服务器获取客户端真实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; } log_format localhost '$remote_addr - $remote_user [$time_local] $request ' '"$status" $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log /data1/logs/www.log localhost ; } }
The above is the relevant tutorial on Nginx reverse proxy and load balancing practice. I hope it can help everyone.
Related recommendations:
Sample code for replacing Nginx server with Tengine in LNMP environment
How to implement reverse proxy using Nginx in php
Detailed explanation of nginx high concurrency configuration parameters
The above is the detailed content of Nginx reverse proxy and load balancing practice. For more information, please follow other related articles on the PHP Chinese website!