需求描述:
一台服务器(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