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端口都要开放
1 2 3 4 5 | 开放端口: 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文件内容如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | upstream www.test.com { #这里的名称要跟proxy_pass内的名称一致 <br> server 127.0 . 0.1: 8080 weight= 1 ; #weight:权重,默认是 1 ,数值越大提供服务的次数就越多 <br> server 127.0 . 0.1: 8081 weight= 1 ; <br> server 127.0 . 0.1: 8083 down; #down表示当前的服务暂不参于负载<br> server 127.0 . 0.1: 8084 backup; #bac kup 其它所有非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 4 k; proxy_buffers 4 32 k; proxy_busy_buffers_size 64 k; proxy_temp_file_write_size 64 k; } } |
接着将创建的vhosts/webapps.conf文件include到nginx.conf文件内。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 | #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"' ; #acce ss_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 koi 8 -r; #acce ss_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 / 50 x.html # error_page 500 502 503 504 / 50 x.html; location = / 50 x.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:
1 | ./nginx -s reload |
在地址栏输入地址看下效果:
把8080端口的Tomcat关闭后再访问:
3.随机启动
通过vi /lib/systemd/system/nginx.service来添加nginx.service文件,并输入如下内容:
1 2 3 4 5 6 7 8 9 10 11 12 13 | [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为可执行:
1 | chmod 755 /lib/systemd/system/nginx.service |
设置开机自启动
1 | systemctl enable nginx.service |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 因为Apifox不支持离线,我果断选择了Apipost!
· 通过 API 将Deepseek响应流式内容输出到前端