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

使用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文件:


#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;
    #第一个服务
    server {
        listen       80; #监听80端口
        server_name jitian.weige.com; #第一个服务名

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        proxy_pass http://jitian.weige.com:8099;#第一个服务转发的路径
        }

        #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;
        #}
    }
    #第二个服务
server {
        listen       80; #默认也监听80端口
        server_name mingtian.weige.com;#第二个服务名

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        proxy_pass http://mingtian.weige.com:8299;#第二个服务转发的路径
        }

        #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;
    #    }
    #}

}


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

4.重启nginx

保存配置文件,重启nginx服务器,进入到安装路径下,我这里是:/usr/local/nginx/sbin,
执行命令:./nginx
查看进程:ps -ef |grep nginx,若出现类似提示,说明启动成功:
在这里插入图片描述
访问第一个项目jintian.weige.com:
在这里插入图片描述
访问第二个项目:mingtian.weige.com:
在这里插入图片描述
测试成功,流程若有不对的,欢迎指正。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
要将多个子域名映射到Tomcat中的不同项目,可以按照以下步骤进行配置: 1. 在Tomcat配置多个虚拟主机 在Tomcat的 `server.xml` 配置文件中,可以添加多个 `<Host>` 元素,每个元素都代表一个虚拟主机。例如: ``` <Engine name="Catalina" defaultHost="localhost"> <Host name="www.example.com" appBase="webapps/example" unpackWARs="true" autoDeploy="true"> <Context path="" docBase="." /> </Host> <Host name="blog.example.com" appBase="webapps/blog" unpackWARs="true" autoDeploy="true"> <Context path="" docBase="." /> </Host> </Engine> ``` 上面的配置表示,将 `www.example.com` 映射到 `webapps/example` 目录下的项目,将 `blog.example.com` 映射到 `webapps/blog` 目录下的项目。 2. 配置DNS解析 将多个子域名解析到服务器的IP地址上,可以通过添加多个A记录或CNAME记录来实现。例如: ``` www.example.com. IN A 192.168.1.100 blog.example.com. IN A 192.168.1.100 ``` 上面的配置表示,将 `www.example.com` 和 `blog.example.com` 都解析到IP地址为 `192.168.1.100` 的服务器上。 3. 配置Apache或Nginx反向代理 如果使用Apache或Nginx作为反向代理服务器,可以将请求转发到不同Tomcat虚拟主机上。例如,在Apache中的配置可以如下: ``` <VirtualHost *:80> ServerName www.example.com ProxyPass / http://localhost:8080/ ProxyPassReverse / http://localhost:8080/ </VirtualHost> <VirtualHost *:80> ServerName blog.example.com ProxyPass / http://localhost:8081/ ProxyPassReverse / http://localhost:8081/ </VirtualHost> ``` 上面的配置表示,将 `www.example.com` 的请求转发到Tomcat的 `www.example.com` 虚拟主机上,将 `blog.example.com` 的请求转发到Tomcat的 `blog.example.com` 虚拟主机上。 完成上述配置后,就可以通过不同的子域名访问不同Tomcat项目了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值