1、到Nginx官网下载相应版本的Nginx nginx-1.12.1.tar.gz
2、解压到 /usr/local/nginx-1.12.1
3、建立软链 ln -s /usr/local/nginx-1.12.1/ /usr/local/nginx
4、进入/usr/local/nginx/conf目录
vim 打开nginx.conf
在http{
.......
include inc/*.conf;
include include/*.conf;
}底部添加红色代码,
内容如下:注意红色部分
#user nobody; worker_processes 1; #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #pid logs/nginx.pid; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; #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; #gzip on; server { listen 80; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / { root html; index index.html index.htm; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } # 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; #} } # 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; # } #} include inc/*.conf; include include/*.conf; }
5、到 /usr/local/nginx/conf/ 目录下创建 两个文件夹 Inc include
6、进入 Inc 添加你所需要的基本配置
fastcgi.conf
##fastcgi fastcgi_connect_timeout 30; fastcgi_send_timeout 60; fastcgi_read_timeout 60; fastcgi_buffer_size 128k; fastcgi_buffers 8 256k; fastcgi_busy_buffers_size 256k; fastcgi_temp_file_write_size 256k; fastcgi_intercept_errors on; upstream php_backend { server 127.0.0.1:1026; }
gzip.conf
## Compression gzip on; ##gzip_static on; gzip_min_length 1024; gzip_buffers 16 8k; gzip_http_version 1.1; gzip_comp_level 9; gzip_types text/plain text/css application/x-javascript application/x-amf text/xml application/xml application/rss+xml text/javascri pt image/gif image/jpeg image/png image/tiff image/x-icon image/x-ms-bmp application/pdf application/x-shockwave-flash application/vnd.wap.xhtml+ xml; gzip_vary on;
logs.conf
log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for" ' '"$host" "$server_port" $request_time'; access_log /home/logs/nginx/access_log main; error_log /home/logs/nginx/error_log;
ssl.conf
#ssl on; #ssl_certificate certs/myssl.crt; #ssl_certificate_key certs/myssl.key; #ssl_ciphers "HIGH:!aNULL:!MD5:!EXPORT56:!EXP"; ## ssl_ciphers "ECDHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES128-SHA256:ECDHE-RS A-AES256-SHA:ECDHE-RSA-AES128-SHA:ECDHE-RSA-DES-CBC3-SHA:AES256-GCM-SHA384:AES128-GCM-SHA256:AES256-SHA256:AES128-SHA256:AES256-SHA:AES128-SHA:DE S-CBC3-SHA:!aNULL:!eNULL:!EXPORT:!DES:!MD5:!PSK:!RC4"; #ssl_protocols TLSv1 TLSv1.1 TLSv1.2; #ssl_prefer_server_ciphers on; #ssl_session_cache shared:SSL:10m; #ssl_session_timeout 5m;
7、进入 /usr/local/nginx/conf/include 进行代理配置
8、具体打单个proxy.conf
upstream www_xiaoga_cn { server 127.0.0.1:8100 max_fails=3 fail_timeout=30s; server 127.0.0.1:8200 max_fails=3 fail_timeout=30s; } server { listen 80; server_name www.xiaoga.cn; access_log /home/logs/nginx/www.xiaoga.cn/access_log main; error_log /home/logs/nginx/www.xiaoga.cn/error_log; log_not_found on; location / { proxy_pass http://www_xiaoga_cn; proxy_set_header Host $host:$server_port; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Real-PORT $remote_port; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_connect_timeout 300; proxy_send_timeout 300; proxy_read_timeout 600; proxy_buffers 4 256k; proxy_busy_buffers_size 256k; proxy_temp_file_write_size 256k; proxy_next_upstream error timeout invalid_header http_502 http_503 http_504; proxy_max_temp_file_size 128m; } }
==================================================
由于每一个proxy.conf都是大度的一个配置文件,不担心配置时出错而影响其他代理配置
9、启动两个Tomcat服务8100 8200
由于域名是假的需要配置host,host配置不多讲,自行百度
测试:
到此实现了一个简单的负载均衡,over!
个人原创,转载请注明出处 https://www.cnblogs.com/xingtangxiaoga/p/9686524.html