1. 程式人生 > >使用nginx配置二級域名

使用nginx配置二級域名

最近想把三個專案配在一個伺服器上,於是想使用nginx配置二級域名實現。

1.域名新增解析

我的是阿里雲的域名,所以首先給自己的域名新增解析。

打算使用 www.codeliu.com , test1.codeliu.com, test2.codeliu.com這三個域名,其中 test1.codeliu.com, test2.codeliu.com作為二級域名。

解析www.codeliu.com

解析test1.codeliu.com

解析test2.codeliu.com

2.準備好三個專案

eclipse新建三個web專案,直接在index.html中寫一點東西能進行區別進行,然後export成war包,傳到伺服器的tomcat webapp目錄下。

3.配置nginx

為了方便,我就直接在nginx.conf進行配置了,沒有重新起配置檔案,如果想重新起配置檔案,可以在nginx.conf使用include進行包含就行。

vim /usr/local/nginx/conf/nginx.conf

使用上面的命令編輯nginx的配置檔案,先把配置檔案中的server註釋掉,然後新增下面的語句

server {
    listen          80;
    server_name     www.codeliu.com;

    location / {
        root    /usr/lib/apache-tomcat-8.5.33/webapps/CodeliuDemo;
        index   index.html index.htm;
    }
}


server {
    listen          80;
    server_name     test1.codeliu.com;

    location / {
        root   /usr/lib/apache-tomcat-8.5.33/webapps/Test1Demo;
        index  index.html index.htm;
    }
}

server {
    listen          80;
    server_name     test2.codeliu.com;

    location / {
        root    /usr/lib/apache-tomcat-8.5.33/webapps/Test2Demo;
        index   index.html index.htm;
    }
}

重啟nginx

/usr/local/nginx/sbin/nginx -t
/usr/local/nginx/sbin/nginx -s reload

這樣配置就完成了,輸入不同的網站就可以顯示不同的專案。

4.遇到的問題

在配置完成之後,訪問出現了403。

4.1 首先看看是否有index檔案

檢查後發現都有,應該不是這個原因。

4.2 再檢查是否是資料夾的許可權問題

chmod -R 777

使用上面的命令後還是不行。

4.3 檢查啟動使用者和nginx工作使用者是否一致

修改nginx.conf的第一行,改成下面這樣

user  root;

重啟nginx,訪問正常。


2018-11-21 15:21再次更新

試了試發現訪問動態專案是404,所以決定一個專案使用一個tomcat,分配不同的埠,這樣一個tomcat掛了不會影響其他的專案。

所以解壓了三個tomcat,分別對應8080,8081,8082埠。修改tomcat的server.conf達到修改埠的目的。要改三個地方

<!-- 這裡我把8005改成8006 -->
<Server port="8005" shutdown="SHUTDOWN">
  <Listener className="org.apache.catalina.startup.VersionLoggerListener" />
<!-- 這裡我把8080改成8081 -->
<Connector port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />
<!-- 這裡我把8009改成8010 -->
<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />

重啟三個tomcat

修改nginx.conf(上面已經配置過,直接在此基礎上修改)

server {
    listen          80;
    server_name     www.qidaochina.com;

    location / {
        proxy_pass    http://127.0.0.1:8081;
        index   index.html index.htm;
    }
}


server {
    listen          80;
    server_name     ccmp.qidaochina.com;

    location / {
        proxy_pass    http://127.0.0.1:8080;
        index  index.html index.htm;
    }
}

server {
    listen          80;
    server_name     xwgk.qidaochina.com;

    location / {
        proxy_pass    http://127.0.0.1:8082;
        index   index.html index.htm;
    }
}

其實就是把root改成了proxy_pass。這樣就能把請求轉發到對應的tomcat去處理。

本來想直接把專案名改成ROOT(tomcat的預設顯示專案),但發現報錯500,於是放棄了這種想法,而是更改ROOT下面的index.jsp.

<%@ page session="false" pageEncoding="UTF-8" contentType="text/html; charset=UTF-8" %>
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
    </head>

    <body>
        <script type="text/javascript">
        	window.location.href="../CodeliuDemo/index.html";
        </script>
    </body>
</html>

這樣就ok了。