🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
# linux下使用nginx反向代理搭建负载均衡 ##1、本地虚拟机搭建 1、在本地搭建三台虚拟机配置分别如下: ~~~ server { listen 81; server_name 127.0.0.1; index index.html index.htm index.php; root /home/wwwroot/hlsc1/; #access_log /home/wwwroot/pigcms/logs/pigaccess.log; rewrite_log on; #error_log /home/wwwroot/pigcms/logs/error.log debug; #access_log /home/wwwroot/pigcms/logs/pigaccess.log; location ~ .*\.(php|php5)?$ { #fastcgi_pass 127.0.0.1:9000; #fastcgi_index index.php; #include fastcgi.conf; include enable-php.conf; } location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ { expires 30d; } location ~ .*\.(js|css)?$ { expires 1h; } #access_log /home/wwwroot/pigcms/logs/default.log; } ~~~ ~~~ server { listen 82; server_name 127.0.0.1; index index.html index.htm index.php; root /home/wwwroot/hlsc2/; #access_log /home/wwwroot/pigcms/logs/pigaccess.log; rewrite_log on; #error_log /home/wwwroot/pigcms/logs/error.log debug; #access_log /home/wwwroot/pigcms/logs/pigaccess.log; location ~ .*\.(php|php5)?$ { #fastcgi_pass 127.0.0.1:9000; #fastcgi_index index.php; #include fastcgi.conf; include enable-php.conf; } location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ { expires 30d; } location ~ .*\.(js|css)?$ { expires 1h; } #access_log /home/wwwroot/pigcms/logs/default.log; } ~~~ ##配置负载均衡的配置参数 `touch fzjh.conf` 加入如下代码 ~~~ user www www;# 用户 用户组 worker_processes 4;#开启的进程 一般是cpu的一倍或者两倍 events{ worker_connections 1024; #最大链接数 } http{ upstream mypro #此处是负载均衡的服务器集群的地址列表 { #ip_hash; #这里有三个参数 #ip_hash按照ip的哈希值分配服务器 这样解决了 session的问题 #fair(第三方) 按后端服务器的响应时间来分配请求,响应时间短的优先分配 #url_hash(第三方)按访问url的hash结果来分配请求,使每个url定向到同一个后端服务器,后端服务器为缓存时比较有效。    server 127.0.0.1:80 weight=2; server 127.0.0.1:81 weight=2; server 127.0.0.1:82 weight=2; } server #提供反向代理的服务监听 { listen 8080; location /{ proxy_pass http://mypro; } } } ~~~ ##加载配置参数 `/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/fzjh.conf` ~~~ killall -9 nginx #杀死nginx的所有进程 service nginx start #启动nginx进程 ~~~ 或者跟随服务器启动如下代码 ~~~ user www www; worker_processes auto; error_log /home/wwwlogs/nginx_error.log crit; pid /usr/local/nginx/logs/nginx.pid; #Specifies the value for maximum file descriptors that can be opened by this process. worker_rlimit_nofile 51200; events { use epoll; worker_connections 51200; multi_accept on; } http { include mime.types; default_type application/octet-stream; server_names_hash_bucket_size 128; client_header_buffer_size 32k; large_client_header_buffers 4 32k; client_max_body_size 50m; sendfile on; tcp_nopush on; keepalive_timeout 60; 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 256k; 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 application/x-javascript text/javascript text/css application/xml application/xml+rss; gzip_vary on; gzip_proxied expired no-cache no-store private auth; gzip_disable "MSIE [1-6]\."; #limit_conn_zone $binary_remote_addr zone=perip:10m; ##If enable limit_conn_zone,add "limit_conn perip 10;" to server section. server_tokens off; #log format log_format access '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" $http_x_forwarded_for'; access_log off; upstream mypro # { #ip_hash; server 127.0.0.1:80 weight=2; server 127.0.0.1:81 weight=2; server 127.0.0.1:82 weight=2; } server { #listen 443 default_server; listen 80; server_name 127.0.0.1; index index.html index.htm index.php; root /home/wwwroot/default; #error_page 404 /404.html; include enable-php.conf; location /nginx_status { stub_status on; access_log off; } location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ { expires 30d; } location ~ .*\.(js|css)?$ { expires 12h; } location ~ /\. { deny all; } #ssl on; #ssl_certificate /usr/local/nginx/conf/server.crt; #ssl_certificate_key /usr/local/nginx/conf/server.key; access_log /home/wwwlogs/access.log access; } server { listen 8080; location /{ proxy_pass http://mypro; } } include vhost/*.conf; } ~~~ ##参考文章 http://www.server110.com/nginx/201403/7113.html