查看: 15|回覆: 0

一台服务器Nginx配置多个域名(一级域名或二级)

[複製鏈接]

4

主題

0

回帖

0

積分

热心网友

金币
0
閲讀權限
220
精華
0
威望
0
贡献
0
在線時間
0 小時
註冊時間
2011-11-25
發表於 2020-2-25 23:40:00 | 顯示全部樓層 |閲讀模式

需求描述:

一台服务器(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;
                }
                # ......  #
        }

至此,问题解决。



来源:https://www.cnblogs.com/martinl/p/12364677.html
回覆

使用道具 舉報

您需要登錄後才可以回帖 登錄 | 立即注册

本版積分規則

相关侵权、举报、投诉及建议等,请发 E-mail:qiongdian@foxmail.com

Powered by Discuz! X5.0 © 2001-2026 Discuz! Team.

在本版发帖返回顶部