1 准备工作
- Nginx部署在192.168.247.100上。
- Tomcat是以Docker启动的,部署在192.168.247.101上。
- 关闭两台机器上的防火墙,防止影响测试:
systemctl stop firewalld
systemctl disable firewalld
2 配置反向代理
docker run -d --name tomcat7 -p 8080:8080 tomcat:7.0.62
docker run -d --name tomcat8 -p 8081:8080 tomcat:8.0.52
- 进入
/usr/local/nginx/conf/目录,修改nginx.conf文件:
cd /usr/local/nginx/conf/
http { ... upstream reverse_proxy { server 192.168.247.101:8080; server 192.168.247.101:8081; } server{ location / { ... proxy_pass http://reverse_proxy; proxy_set_header Host $host; ... } ... }}
# 安全问题,建议使用nobody,不要用root#user nobody;# worker数和服务器的CPU数量相等为最佳worker_processes 1;# worker绑定CPU(1个worker绑定4个CPU)# worker_cpu_affinity 0001 0010 0100 1000# 错误日志#error_log logs/error.log;#error_log logs/error.log notice;#error_log logs/error.log info;#pid logs/nginx.pid;events { # 每个worker进程所能建立连接的最大值 worker_connections 1024; # Nginx支持IO多路复用 # use epoll; # 当一个worker抢占到一个连接时,是否尽可能的让其获取更多的链接,默认是off。 # multi_accept on; # 默认是on,开启Nginx的抢占锁机制 # accept_mutex on;}http { # 当Web服务器收到静态的资源文件请求的时候,根据请求文件的后缀名在服务器的MIME配置文件中找到对应的MIME Type,再根据MIME TYPE设置HTTP Response的Content-Type,然后浏览器根据Content-Type的值处理文件 include mime.types; # 如果不能从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; # 开启从磁盘直接到网络的文件传输,适用于有大文件上传下载的情况,提高IO的效率 sendfile on; #tcp_nopush on; # 一个请求完成之后还要保持连接多久,默认为0,表示完成请求后直接关闭连接 #keepalive_timeout 0; keepalive_timeout 65; # 开启或关闭gzip模块 #gzip on; # 设置允许压缩的页面最小字节数,页面字节数从header头中的Content-Type中进行获取 #gzip_min_length 1k; # gzip的压缩比,1 压缩比最小处理速度最快,9压缩比最大但处理速度最慢(传输速度快但比较消耗CPU) #gzip_comp_level 4; # 匹配MIME类型进行压缩,(无论是否指定)"text/html"类型总是会被压缩的 #gzip_types types text/plain text/css application/json application/x-javascript text/xml # 动静分离 # 服务器端静态资源缓存,最大缓存到内存中的文件,不活跃期限 #open_file_cache max=655350 inactive=20s; # 活跃期限内最少使用的次数,否则视为不活跃 #open_file_cache_min_uses 2; # 验证缓存是否活跃的时间间隔 # open_file_cache_valid 30s; # 反向代理 upstream reverse_proxy { # 1.轮询(默认) 每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,则自动剔除 # 2.指定权重 指定轮询几率,weight和访问比率成正比,用于后端服务器性能不均的情况。server 192.168.247.101:8080 weight=1; # 3.IP绑定 ip_hash 每个请求按照访问ip的hash的结果分配,这样每个访客固定一个后端服务器,可以解决Session的问题。 # 4.备机方式 backup 正常情况下不访问设置为backup的备机,只有当所有非备机全部宕机的情况下,服务才会进备机 # 5. fair 第三方 按后端服务器的响应时间来分配请求,响应时间短的优先分配 # 6. url_hash 第三方 按访问URL的hash的结果来分配请求,使得每一个URL都定向到同一个后端服务器,后端服务器为缓存时比较有效 server 192.168.247.101:8080; server 192.168.247.101:8081; } server { # 监听端口 listen 80; # 服务名 server_name localhost; # 字符集 #charset koi8-r; #access_log logs/host.access.log main; # 路径匹配 # location [=|~|~*|^~] /uri/ { _ } # = 精确匹配 # ~ 正则匹配,区分大小写 # ~* 正则匹配,不区分大小写 # ^~ 关闭正则匹配 # 匹配规则 # 1. 所有匹配分两个阶段,第一个叫做普通匹配,第二个叫做正则匹配。 # 2. 普通匹配,首先通过"="来匹配完全精确的location # 2.1 如果没有精确匹配到,那么按照最大前缀匹配的原则,来匹配location # 2.2 如果匹配到的location有^~,则以此location为匹配最终结果,如果没有会把匹配结果暂存,继续进行正则匹配 # 3. 正则匹配,依次从上到下匹配前缀是~或~*的location,一旦匹配成功依次,则立刻以此location为准,不再向下继续进行正则匹配 # 4. 如果正则匹配度不成功,则继续使用之前暂存的普通匹配的location location / { # 匹配任何查询,因为所有请求都以/开头,但是正则表达式罪责和长的块规则将被优先和查询匹配 # 反向代理的路径 proxy_pass http://reverse_proxy; proxy_set_header Host $host; # 定义服务器的默认网站根目录位置 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; # } #}}
cd /usr/local/nginx/./nginx
- 如果Nginx启动了,那么就让Nginx加载一下配置文件:
cd /usr/local/nginx/./nginx -s reload