环境:
Windows7 64位
nginx/Windows-1.12.0
Tomcat(Springboot中内置)
Nginx
nginx使用
- 在官网下载,解压到磁盘e。
启动:
e:\nginx-1.12.0>start nginx 或 e:\nginx-1.12.0>>nginx.exe
注:建议使用第一种,第二种会使你的cmd窗口一直处于执行中,不能进行其他命令操作。
默认使用80端口,日志见文件夹nginx\logs文件夹。
停止:
e:\nginx-1.12.0>>nginx.exe -s stop 或 e:\nginx-1.12.0>>nginx.exe -s quit
注:stop是快速停止nginx,可能并不保存相关信息;quit是完整有序的停止nginx,并保存相关信息。
重新载入Nginx:
e:\nginx-1.12.0>>nginx.exe -s reload
当配置信息修改,需要重新载入这些配置时使用此命令。
修改Nginx核心配置文件nginx.conf
Nginx的基本配置:
- 监听端口一般都为http端口:80;
- 域名可以有多个,用空格隔开:例如 server_name www.wsyw126.cc www.wsyw126.cn;
负载均衡列表基本配置:
- location/ {}:对什么样的后缀进行负载均衡请求,假如我们要对所有的aspx后缀的文件进行负载均衡时,可以这样写:location ~ .*.aspx$ {}
- proxy_pass:请求转向自定义的服务器列表,这里我们将请求都转向标识为http://test.com; (需要加上http://)的负载均衡服务器列表;
- 在负载均衡服务器列表的配置中,weight是权重,可以根据机器配置定义权重(如果某台服务器的硬件配置十分好,可以处理更多的请求,那么可以为其设置一个比较高的weight;而有一台的服务器的硬件配置比较差,那么可以将前一台的weight配置为weight=2,后一台差的配置为weight=1)。weigth参数表示权值,权值越高被分配到的几率越大;
配置如下:
#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; #自己定义的集群名和对应的服务器地址 upstream test.com {#服务器集群名字 server 127.0.0.1:8080 weight=1; server 127.0.0.1:8081 weight=2; } server { listen 80; #监听80端口 server_name localhost;#当前服务的域名,本测试为本地测试 #charset koi8-r; #access_log logs/host.access.log main; #location / { # root html; # index index.html index.htm; #} location / { proxy_pass http://test.com; proxy_redirect default; } #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; # } #} }
服务器demo
SpringBoot 项目:demo
- 一个指定端口号为8080,一个指定为8081。
- 打包生成jar包。
测试
- 然后运行java -jar demo-0.0.1-SNAPSHOT.jar 和demo-0.0.2-SNAPSHOT.jar。
- 运行nginx。
- 访问http://localhost/webapi/login
说明
由于权重的不同,在我们不断刷新页面的过程中,显示server two的可能性会大一些
参考资料:
网上资源
备注:
转载请注明出处:http://blog.csdn.net/wsyw126/article/details/71242675
作者:WSYW126