nginx配置:一个tomcat下多个不同项目的访问

雨点打透心脏的1/2处 2023-02-14 01:46 396阅读 0赞

使用nginx来代理一个服务器下的不同项目

前言:上一篇文章中,我实现了在同一tomcat下部署不同的项目,通过IP+指定端口可以访问到各自项目,那么新的需求来了:使用IP+端口太麻烦,而且这样的URL(例如作为扫码跳转地址时)在微信浏览器中,会出现这样的提示:

在这里插入图片描述

这样用户体验不好,敏感信息也多,如何通过域名,来访问这同一服务器下的一个tomcat中部署的两个项目,而且做到访问如丝般润滑呢?是时候祭出Nginx了

1.安装nginx:
安装和配置途中也遇到不少的坑,具体的安装可以参考这篇文章,非常感谢这位博主:Linux下nginx的安装以及环境配置

2.配置域名
安装好nginx之后,再来明确一下甲方需求:需要用不同的域名,来访问这一台服务器下,同一个tomcat中部署的两个项目,先在心中默念三遍,再开始配置:
1.登陆阿里云,进入域名解析控制台,我现在有一个一级域名:weige.com,点击设置
在这里插入图片描述

点击添加记录,设置内容

在这里插入图片描述

配置信息
在这里插入图片描述

如此这般,我便配置好了第一个域名:jintian.weige.com—————>192.163.0.2(我的服务器地址),如法炮制,第二个域名:mingtian.weige.com——->192.163.0.2(我的服务器地址)

3.修改nginx配置文件
我配置好的nginx.conf文件:

  1. #user nobody;
  2. worker_processes 1;
  3. #error_log logs/error.log;
  4. #error_log logs/error.log notice;
  5. #error_log logs/error.log info;
  6. #pid logs/nginx.pid;
  7. events {
  8. worker_connections 1024;
  9. }
  10. http {
  11. include mime.types;
  12. default_type application/octet-stream;
  13. #log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  14. # '$status $body_bytes_sent "$http_referer" '
  15. # '"$http_user_agent" "$http_x_forwarded_for"';
  16. #access_log logs/access.log main;
  17. sendfile on;
  18. #tcp_nopush on;
  19. #keepalive_timeout 0;
  20. keepalive_timeout 65;
  21. #gzip on;
  22. #第一个服务
  23. server {
  24. listen 80; #监听80端口
  25. server_name jitian.weige.com; #第一个服务名
  26. #charset koi8-r;
  27. #access_log logs/host.access.log main;
  28. location / {
  29. root html;
  30. index index.html index.htm;
  31. proxy_pass http://jitian.weige.com:8099;#第一个服务转发的路径
  32. }
  33. #error_page 404 /404.html;
  34. # redirect server error pages to the static page /50x.html
  35. #
  36. error_page 500 502 503 504 /50x.html;
  37. location = /50x.html {
  38. root html;
  39. }
  40. # proxy the PHP scripts to Apache listening on 127.0.0.1:80
  41. #
  42. #location ~ \.php$ {
  43. # proxy_pass http://127.0.0.1;
  44. #}
  45. # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
  46. #
  47. #location ~ \.php$ {
  48. # root html;
  49. # fastcgi_pass 127.0.0.1:9000;
  50. # fastcgi_index index.php;
  51. # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
  52. # include fastcgi_params;
  53. #}
  54. # deny access to .htaccess files, if Apache's document root
  55. # concurs with nginx's one
  56. #
  57. #location ~ /\.ht {
  58. # deny all;
  59. #}
  60. }
  61. #第二个服务
  62. server {
  63. listen 80; #默认也监听80端口
  64. server_name mingtian.weige.com;#第二个服务名
  65. #charset koi8-r;
  66. #access_log logs/host.access.log main;
  67. location / {
  68. root html;
  69. index index.html index.htm;
  70. proxy_pass http://mingtian.weige.com:8299;#第二个服务转发的路径
  71. }
  72. #error_page 404 /404.html;
  73. # redirect server error pages to the static page /50x.html
  74. #
  75. error_page 500 502 503 504 /50x.html;
  76. location = /50x.html {
  77. root html;
  78. }
  79. # proxy the PHP scripts to Apache listening on 127.0.0.1:80
  80. #
  81. #location ~ \.php$ {
  82. # proxy_pass http://127.0.0.1;
  83. #}
  84. # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
  85. #
  86. #location ~ \.php$ {
  87. # root html;
  88. # fastcgi_pass 127.0.0.1:9000;
  89. # fastcgi_index index.php;
  90. # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
  91. # include fastcgi_params;
  92. #}
  93. # deny access to .htaccess files, if Apache's document root
  94. # concurs with nginx's one
  95. #
  96. #location ~ /\.ht {
  97. # deny all;
  98. #}
  99. }
  100. # another virtual host using mix of IP-, name-, and port-based configuration
  101. #
  102. #server {
  103. # listen 8000;
  104. # listen somename:8080;
  105. # server_name somename alias another.alias;
  106. # location / {
  107. # root html;
  108. # index index.html index.htm;
  109. # }
  110. #}
  111. # HTTPS server
  112. #
  113. #server {
  114. # listen 443 ssl;
  115. # server_name localhost;
  116. # ssl_certificate cert.pem;
  117. # ssl_certificate_key cert.key;
  118. # ssl_session_cache shared:SSL:1m;
  119. # ssl_session_timeout 5m;
  120. # ssl_ciphers HIGH:!aNULL:!MD5;
  121. # ssl_prefer_server_ciphers on;
  122. # location / {
  123. # root html;
  124. # index index.html index.htm;
  125. # }
  126. #}
  127. }

nginx的默认端口是80,我们这里不做修改,我们解析的域名也默认访问的是80端口,这样,当使用域名请求服务器时,请求会先到达nginx,nginx根据不同的服务名,将请求转发到不同的项目,我的理解是这样,若有不对,欢迎吐槽。

4.重启nginx
保存配置文件,重启nginx服务器,进入到安装路径下,我这里是:/usr/local/nginx/sbin,
执行命令:./nginx
查看进程:ps -ef |grep nginx,若出现类似提示,说明启动成功:
在这里插入图片描述

访问第一个项目jintian.weige.com:
在这里插入图片描述

访问第二个项目:mingtian.weige.com:

在这里插入图片描述 测试成功,流程若有不对的,欢迎指正。

发表评论

表情:
评论列表 (有 0 条评论,396人围观)

还没有评论,来说两句吧...

相关阅读