1.准備兩個Tomcat
配置兩個Tomcat一個端口是8080另外一個端口是8081,分別在webapps下面添加一個測試用的web項目,修改index.jsp文件,8080端口的index.jsp頁面加入:
This page is from 8080 port
8081的端口的index.jsp加入:
This page is from 8081 port
之后啟動兩個Tomcat,8080端口跟8081端口都要開放
開放端口: firewall-cmd --zone=public --add-port=8080/tcp --permanent 查看開放的端口: firewall-cmd --list-ports
2.配置Nginx
使用whereis nginx命令找到nginx所在的目錄並進入目錄內的conf文件夾找到ngnix.conf配置文件,在conf文件夾下創建一個vhosts文件夾並在里面創建一個webapp.conf文件內容如下:
upstream www.test.com { #這里的名稱要跟proxy_pass內的名稱一致
server 127.0.0.1:8080 weight=1; #weight:權重,默認是1,數值越大提供服務的次數就越多
server 127.0.0.1:8081 weight=1;
server 127.0.0.1:8083 down; #down表示當前的服務暫不參於負載
server 127.0.0.1:8084 backup; #backup 其它所有非backup機器down或者忙時,才會請求backup機器。
}
server {
listen 80;
server_name www.test.com;
access_log logs/bbs.access.log;
error_log logs/bbs.error.log;
#root html;
#index index.html index.htm index.jsp index.php;
location / {
proxy_pass http://www.test.com; #這里的http://www.test.com與上面的upstream要一樣
#Proxy Settings
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
proxy_max_temp_file_size 0;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
}
}
接着將創建的vhosts/webapps.conf文件include到nginx.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;
include vhosts/webapp.conf;#這里把webapp.conf文件include進來
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;
#}
}
重新加載Nginx:
./nginx -s reload
在地址欄輸入地址看下效果:

把8080端口的Tomcat關閉后再訪問:

3.隨機啟動
通過vi /lib/systemd/system/nginx.service來添加nginx.service文件,並輸入如下內容:
[Unit] Description=nginx 1.13.7 After=network.target [Service] Type=forking ExecStart=/usr/local/nginx/sbin/nginx ExecReload=/usr/local/nginx/sbin/nginx -s reload ExecStop=/usr/local/nginx/sbin/nginx -s quit PrivateTmp=true [Install] WantedBy=multi-user.target
注意:如果不是安裝在/usr/local/nginx/目錄下,請根據實際安裝路徑修改ExecStart、ExecReload、ExecStop中的值。
更改nginx.service為可執行:
chmod 755 /lib/systemd/system/nginx.service
設置開機自啟動
systemctl enable nginx.service
