阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

使用nginx发布tomcat站点

180次阅读
没有评论

共计 1952 个字符,预计需要花费 5 分钟才能阅读完成。

之前我们在访问的时候使用的都是类似 http://serverip/test 或者 http://serverip/jpress 这种字样的 URL,使用起来比较麻烦,所以呢,我们可以将 tomcat 和 nginx 结合在一起,可以通过 nginx 以下功能发布:

  • 使用 nginx url 重写
  • 使用 nginx 的反向代理功能

一、部署 tomcat 网站

通过部署两个 tomcat 站点,分别采用 nginx url rewrite 方法和反向代理发布。

  • 设置 tomcat1
[root@zutuanxue ~]# cd /opt/tomcat1/webapps/ [root@zutuanxue webapps]# mv ROOT tomcat [root@zutuanxue webapps]# mv jpress ROOT
  • 设置 tomcat2
[root@zutuanxue ~]# cd /opt/tomcat2/webapps/ [root@zutuanxue webapps]# mv ROOT tomcat [root@zutuanxue webapps]# mv test ROOT

二、使用 rewrite 实现

2.1、部署 nginx

[root@zutuanxue ~]# dnf install nginx -y

2.2、设置 nginx 配置文件

[root@zutuanxue ~]# vim /etc/nginx/nginx.conf [root@zutuanxue ~]# sed -i '/#/d' /etc/nginx/nginx.conf [root@zutuanxue ~]# sed -i '/^$/d' /etc/nginx/nginx.conf server {listen 80; listen [::]:80; server_name www.a.com; location / {rewrite ^/$ http://127.0.0.1:8080/jpress break; } } server {listen 80; listen [::]:80;127.0.0.1 server_name www.b.com; location / {rewrite ^/$ http://127.0.0.1:8081/test break; } } [root@zutuanxue ~]# systemctl restart nginx

2.3、修改测试机的 hosts 文件

[root@zutuanxue conf]# vim /etc/hosts 192.168.98.200 www.a.com 192.168.98.200 www.b.com

2.4、打开浏览器直接访问域名测试

测试方法:
打开浏览器输入之前设置好的域名
http://www.a.com
http://www.b.com
查看是否能访问到对应的网站内容,能看到说明实验成功。

三、使用反向代理实现

3.1、设置 nginx 配置文件

user nginx; worker_processes auto; error_log /var/log/nginx/error.log; pid /run/nginx.pid; include /usr/share/nginx/modules/*.conf; events {worker_connections 1024; } http {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 /var/log/nginx/access.log main; sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout 65; types_hash_max_size 2048; include /etc/nginx/mime.types; default_type application/octet-stream; include /etc/nginx/conf.d/*.conf; server {listen 80; listen [::]:80; server_name www.a.com; location / {proxy_pass http://127.0.0.1:8080; } } server {listen 80; listen [::]:80; server_name www.b.com; location / {proxy_pass http://127.0.0.1:8081; } } }

3.2、打开浏览器直接访问域名测试

测试方法:
打开浏览器输入之前设置好的域名
http://www.a.com
http://www.b.com
查看是否能访问到对应的网站内容,能看到说明实验成功。

正文完
星哥说事-微信公众号
post-qrcode
 
星锅
版权声明:本站原创文章,由 星锅 2022-06-09发表,共计1952字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中