Rumah > Artikel > pembangunan bahagian belakang > Nginx反向代理设置总结
负载均衡技术有很多种,常用的四/七层负载均衡技术包含很多,Nginx反向代理就是其中的一种方案。
以下的配置就是Nginx反向代理配置样例:
user www www; worker_processes 10; error_log /data1/logs/error.log crit; pid /usr/local/webserver/nginx/nginx.pid; worker_rlimit_nofile 51200; events { use epoll; worker_connections 51200; } http { include mime.types; default_type application/octet-stream; #charset utf-8 server_names_hash_buckets_size 128; client_header_buffer_size 32k; large_client_header_buffers 4 32k; #log_format main '$remote_addr - $remote_user [$time_local] "$request" ' # '$status $body_bytes_sent "$http_referer" ' # '"$http_user_agent" "$http_x_forwarded_for"'; #access_log logs/access.log main; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; tcp_nodelay on; fastcgi_connect_timeout 300; fastcgi_send_timeout 300; fastcgi_read_timeout 300; fastcgi_buffer_size 64k; fastcgi_buffers 4 64k; fastcgi_busy_buffers_size 128k; fastcgi_temp_file_write_size 128k; gzip on; gzip_min_length 1k; gzip_buffers 4 16k; gzip_http_version 1.1; gzip_comp_level 2; gzip_types text/plain application/javascript text/css application/xml; gzip_vary on; client_max_body_size 300m; client_body_buffer_size 128k; proxy_connect_timeout 600; proxy_read_timeout 600; proxy_send_timeout 600; proxy_buffer_size 16k; proxy_buffers 4 32k; proxy_busy_buffers_size 64k; proxy_temp_file_write_size 64k; upstream php_server_pool { server 192.168.1.10:80 weight=4 max_fails=2 fail_timeout=30s; server 192.168.1.11:80 weight=4 max_fails=2 fail_timeout=30s; server 192.168.1.12:80 weight=4 max_fails=2 fail_timeout=30s; } upstream message_server_pool { server 192.168.1.13:3245; server 192.168.1.14:3245 down; } upstream php_server_pool { server 192.168.1.15:80 weight=1 max_fails=2 fail_timeout=30s; server 192.168.1.16:80 weight=1 max_fails=2 fail_timeout=30s; server 192.168.1.17:80 weight=1 max_fails=2 fail_timeout=30s; server 192.168.1.18:80 weight=1 max_fails=2 fail_timeout=30s; } server { listen 80; server_name www.yourdomain.com; #charset koi8-r; access_log /data1/logs/www.yourdomain.com_access.log; location / { proxy_next_upstream http_502 http_504 error timeout invalid_header; proxy_pass http://php_server_pool; proxy_set_header Host www.yourdomain.com; proxy_set_header X-Forwarded-For $remote_addr; } # proxy the PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ \.php$ { # proxy_pass http://127.0.0.1; #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # #location ~ \.php$ { # root html; # fastcgi_pass 127.0.0.1:9000; # fastcgi_index index.php; # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; # include fastcgi_params; #} # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /\.ht { # deny all; #} } server { listen 80; server_name www1.yourdomain.com; localtion /message/ { proxy_pass http://message_server_pool; proxy_set_header Host $host; } localtion / { proxy_pass http://php_server_pool; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $remote_addr; } access_log /data1/logs/message.yourdomain.com_access.log; } server { listen 80; server_name bbs.yourdomain.com *.bbs.yourdomain.com; location / { proxy_pass http://bbs_server_pool; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $remote_addr; } access_log off; } # another virtual host using mix of IP-, name-, and port-based configuration # #server { # listen 8000; # listen somename:8080; # server_name somename alias another.alias; # location / { # root html; # index index.html index.htm; # } #} # HTTPS server # #server { # listen 443 ssl; # server_name localhost; # ssl_certificate cert.pem; # ssl_certificate_key cert.key; # ssl_session_cache shared:SSL:1m; # ssl_session_timeout 5m; # ssl_ciphers HIGH:!aNULL:!MD5; # ssl_prefer_server_ciphers on; # location / { # root html; # index index.html index.htm; # } #} }以上的配置中,upstream这个是用来配置一组可以在proxy_pass或者fastcgi_pass中使用的一组代理服务器,默认的负载均衡方式是轮询。server用来指定服务器的名称和参数,名称可以是一个域名、一个IP地址、端口号或者UNIX Socket。
proxy_pass和fastcgi_pass可以设置反向代理的upstream集群。
proxy_set_header用于向反向代理的服务器发送请求时增加指定的Header信息。使用Host参数是为了让后端服务器知道由哪个虚拟主机来处理,使用X-Forwarded-For是为了让后端服务器可以通过$_SERVER["HTTP_X_FORWARDED_FOR"]获取到用户的真实IP。
以上就介绍了Nginx反向代理设置总结,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。