需求描述:
一台服務器(ip: 39.105.1xx.xx)上啟nginx,然后配置多個server,分別將不同一級域名或二級域名。
實現方法(不說廢話了,直接上代碼,看懂的來):
注意我是兩個一級域名(.com和.cn)
server {
listen 80;
server_name testapp.com;
location / {
root /usr/share/nginx/html/official_web/;
index index.html index.htm;
# try_files $uri $uri/ /index.html;
}
}
server {
listen 80;
server_name testapp.cn;
location / {
root /usr/share/nginx/html/official_web/zhoubianbaoapp;
index index.html index.htm;
# try_files $uri $uri/ /index.html;
}
}
另外,如果是多個二級域名,也是跟上面一模一樣的。(親測)
延展問題:在testapp.com下配置oms系統,訪問路徑http://testapp.com/oms
發現配置的 http://testapp.com 可以訪問,但是http://testapp.com/oms訪問不了,報錯404。
起初的配置是這樣的(錯誤版):
server {
listen 80;
server_name testapp.com;
location / {
root /usr/share/nginx/html/official_web/;
index index.html index.htm;
# try_files $uri $uri/ /index.html;
}
### oms ###
location /oms {
root /usr/share/nginx/html/oms;
index index.html index.htm;
# try_files $uri $uri/ /oms/index.html;
}
# ...... #
}
搜了一下問題在於:除了location / 可以用root,其余都要用alias(別名)來設置項目的在服務器上的路徑。
修改后:
server {
listen 80;
server_name testapp.com;
location / {
root /usr/share/nginx/html/official_web/;
index index.html index.htm;
# try_files $uri $uri/ /index.html;
}
### oms ###
location /oms {
alias /usr/share/nginx/html/oms; #注意就是這一行
index index.html index.htm;
# try_files $uri $uri/ /oms/index.html;
}
# ...... #
}
至此,問題解決。