1. 程式人生 > >同一個伺服器繫結多個域名,利用Nginx進行域名重定向

同一個伺服器繫結多個域名,利用Nginx進行域名重定向

背景:同一個伺服器繫結多個域名,利用Nginx進行域名重定向。

首先:tomcat進行部署網站後臺介面,進行重設9090埠(可根據需要自定義,但是注意外網訪問埠,需在阿里雲上配置開放埠)。

 

Tomcat版本8.044,Nginx版本nginx-1.14.1

Tomcat配置檔案如下:

 

     <Host appBase="webapps" autoDeploy="true" name="localhost" unpackWARs="true">

            <Alias>域名1</Alias>

           <Alias>域名2</Alias>

      <Context path="" docBase="\mscs" debug="0" reloadable="True" />

        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" pattern="%h %l %u %t &quot;%r&quot; %s %b" prefix="localhost_access_log" suffix=".txt"/>

         </Host>

 

將專案java介面專案匯出war包,放在webapps即可

 

 

Nginx配置如下:

 

    server {

        listen       80;

        server_name 域名2 域名1 ;

        if  ( $host != '域名2'  )  {

        rewrite ^/(.*)$ http:// 域名2/$1 permanent;

       }

       

     

        location / {

            root   html/nginx中前端專案名稱;

            index  index.html index.htm;

        }

             

              location /tomcat中後臺專案名稱1/ {

                     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_pass http://127.0.0.1:9090;

              }

             

                     location /tomcat中後臺專案名稱2/ {

                     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_pass http://127.0.0.1:9090;

              }

 

        location = /50x.html {

            root   html;

        }

 

    }

}

 

將網站部署在nginx下面,

對應nginx.conf 配置

Nginx進行域名重定向,域名2和域名1都重定向到域名2上

 

進入nginx命令視窗

進入到nginx.conf中 shift 按住  點選右鍵

Nginx 常用操作命令:

停止:nginx.exe -s stop

殺掉程序:taskkill /F /IM nginx.exe > nul

更新:nginx.exe -s reload

啟動:start nginx