Tomcat + Nginx域名配置方法 或 直接通过Tomcat解析域名或

一、Tomcat + Nginx域名配置方法

如何通过nginx代理的方式进行域名访问

找到nginx/conf/nginx.conf,做如下关键配置:

upstream xx{ #配置upstream节点,这里节点名为“xx”
  server 116.255.111.111:8080;
 }
 server{
  listen 80;
  server_name www.xxx.xx; #这里配置nginx需要代理的域名
  location / {
    proxy_pass http://xx; #指定反向代理为上面配置的那个upstream节点“xx”
    proxy_set_header Host $http_host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  }
 }

修改tomcat server.xml文件
    
<Host name="xxx.xxx.xxx" appBase="webapps" unpackWARs="true" autoDeploy="true">
  <Context path="" docBase="/home/web/xxx" reloadable="true" crossContext="true" />
</Host>

注意红色字体部分要注意 name为你的域名  path要为空否者无法访问到这个项目

二、Tomcat解析域名

将Tomcat的server.xml的服务端口默认为8080改为80(这样访问时就不用输入端口了):

<Connector port="80" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" />


更改 server.xml中Host的名称为自己的域名

<Host name="自己的域名"  appBase="webapps" unpackWARs="true" autoDeploy="true"
            xmlValidation="false" xmlNamespaceAware="false">

    <Context docBase="项目路径" path="" reloadable="true"/>
</Host>

Tomcat+Nginx解析域名

通过nginx代理的方式进行域名访问,找到nginx/conf/nginx.conf,做如下关键配置:

upstream xx{ #配置upstream节点,这里节点名为“xx”

  server 127.0.0.1:8080;

 }

 server{

  listen 80;

  server_name www.xxx.xx; #这里配置nginx需要代理的域名

  location / {

    proxy_pass http://xx; #指定反向代理为上面配置的那个upstream节点“xx”

    proxy_set_header Host $http_host;

    proxy_set_header X-Real-IP $remote_addr;

    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

  }

三、nginx+tomcat单个域名及多个域名配置

nginx做负载均衡的优势网上有很多介绍资料,这里我不再多做介绍。因为有很多系统要部署,涉及到域名、二级域名、多个域名等的部署。在实际的部署由于对nginx的不够熟悉,遇到过很多坑,其中这种多域名的配置,xxxx.com转发到www.xxxx.com、访问域名转发到tomcat里的项目等,现在先总结一部坑的解决办法。

如将xxxx.com这个域名指向8082端口里的tomcat项目,在做这个介绍前先讲个插曲,如访问xxxx.com需转向到www.xxxx.com,这一点很多人都会忽略。

现在如果要部署中台、后台、金融系统,找到nginx/conf/nginx.conf,修改配置:

upstream web{
  server localhost:8082;
}
upstream admin{
  server localhost:8083;
}
upstream finance{
  server localhost:8084;
}
server {
  listen    80;
  server_name finance.xxxx.com;
  #charset koi8-r;
  #access_log logs/host.access.log main;
  location / {
    proxy_pass http://finance;
    proxy_set_header Host $http_host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  }
  #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;
  }
}
server {
  listen    80;
  server_name www.xxx.com;
  #charset koi8-r;
  #access_log logs/host.access.log main;
  location / {
    proxy_pass http://web;
    proxy_set_header Host $http_host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  }
  #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 {
  server_name xxxx.com;
  rewrite ^(.*) http://www.xxxx.com$1 permanent;
}
server {
  listen    80;
  server_name admin.xxxx.com;
  #charset koi8-r;
  #access_log logs/host.access.log main;
  location / {
    proxy_pass http://admin;
    proxy_set_header Host $http_host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  }
  #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;
  #}
}

 上面的配置还包括了访问xxxx.com转向www.xxxx.com的配置,如下:

server {
   server_name xxxx.com;
   rewrite ^(.*) http://www.xxxx.com$1 permanent;
 }

nginx的基本配置大致就是这样,如果绑定多个域名(不管是一级域名还是二级域名),需配置多个server,你会发现这几个server配置都差不多,主要是更改server_name及proxy_pass指向即可。upstream节点其实就是代理服务的访问路径。

如果此时访问域名,你会发现nginx的配置生效了,只是目前显示的是tomcat的默认界面。nginx的配置基本就这样了,接下来对tomcat做些配置的修改。找到tomcat里的conf/server.xml,注释掉默认的Host配置,添加如下Host配置:

<Host name="localhost" appBase="E:\tomcat\apache-tomcat-8.0.35-8082\webapps\web" deployOnStartup ="false" autoDeploy="false" unpackWARs="true">
       <Context path="/" docBase="E:\tomcat\apache-tomcat-8.0.35-8082\webapps\web" />
       <Valve  className="org.apache.catalina.valves.AccessLogValve"
       directory="logs"   prefix="localhost_access_log"  suffix=".txt"
       pattern="%h %l %u %t "%r" %s %b"  />
</Host>

以上是windows服务器下的配置,如为linux,只需更改appBase和docBase,指向项目的路径。tomcat的配置也已经完成,重启tomcat,访问域名就指向了tomcat里的项目。

或者

//nginx + tomcat 单个域名及多个域名的配置
//修改nginx的配置文件,linux默认路径 /usr/local/nginx/conf/nginx.conf


//prot为8082的web系统
upstream web{
server localhost:8082;
}
server {
listen 80;
server_name www.xxx.com;
location / {
proxy_pass http://web;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
//prot为8084的finance系统
upstream finance{
server localhost:8084;
}
server {
listen 80;
server_name finance.xxxx.com;
location / {
proxy_pass http://finance;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
//prot为8083的admin系统
upstream admin{
server localhost:8083;
}
server {
listen 80;
server_name admin.xxxx.com;
location / {
proxy_pass http://admin;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}


//访问xxxx.com转向www.xxxx.com的配置
server {
server_name xxxx.com;
rewrite ^(.*) http://www.xxxx.com$1 permanent;
}

nginx的基本配置大致就是这样,如果绑定多个域名(不管是一级域名还是二级域名),需配置多个server,你会发现这几个server配置都差不多,主要是更改server_name及proxy_pass指向即可。upstream节点其实就是代理服务的访问路径。
如果此时访问域名,你会发现nginx的配置生效了,只是目前显示的是tomcat的默认界面。nginx的配置基本就这样了,接下来对tomcat做些配置的修改。找到tomcat里的conf/server.xml,注释掉默认的Host配置,添加如下Host配置:
<Host name="localhost" appBase="E:\tomcat\apache-tomcat-8.0.35-8082\webapps\web" deployOnStartup ="false" autoDeploy="false" unpackWARs="true">
<Context path="/" docBase="E:\tomcat\apache-tomcat-8.0.35-8082\webapps\web" />
<Valve className="org.apache.catalina.valves.AccessLogValve"
directory="logs" prefix="localhost_access_log" suffix=".txt"
pattern="%h %l %u %t "%r" %s %b" />
</Host>
以上是windows服务器下的配置,如为linux,只需更改appBase和docBase,指向项目的路径。tomcat的配置也已经完成,重启tomcat,访问域名就指向了tomcat里的项目。

//Nginx同一个域名配置多个项目

1.nginx按不同的目录分发给不同的项目:
server {

listen 80;

server_name example.com;


location ^~ /project1 {

proxy_pass http://localhost:8081;

proxy_set_header Host $host;

proxy_set_header X-Real-IP $remote_addr;

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

}
 

location ^~ /project2 {

proxy_pass http://localhost:8082;

proxy_set_header Host $host;

proxy_set_header X-Real-IP $remote_addr;

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

}


location / {

proxy_pass http://localhost:8080;

proxy_set_header Host $host;

proxy_set_header X-Real-IP $remote_addr;

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

}

}

这里配置了三个项目:


http://example.com/project1路径分发到http://localhost:8081
http://example.com/project2路径分发到http://localhost:8082

其他路径分发到http://localhost:8080


2.启用二级域名,不同的项目分配不同的二级域名

server {

listen 80;

server_name example.com;


location / {

proxy_pass http://localhost:8080;

proxy_set_header Host $host;

proxy_set_header X-Real-IP $remote_addr;

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

}

}

project1


server {

listen 80;

server_name project1.example.com;

location / {

proxy_pass http://localhost:8081;

proxy_set_header Host $host;

proxy_set_header X-Real-IP $remote_addr;

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

}

}

project2

server {

listen 80;

server_name project2.example.com;

location / {

proxy_pass http://localhost:8082;

proxy_set_header Host $host;

proxy_set_header X-Real-IP $remote_addr;

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

}

}

注意:这三个项目属于不同的域名,项目之间通过http访问会存在跨域问题。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值