乡下的孩子早当家 發表於 2020-2-25 23:40:00

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

<h2 id="需求描述">需求描述:</h2>
<p>一台服务器(ip: 39.105.1xx.xx)上启nginx,然后配置多个server,分别将不同一级域名或二级域名。</p>
<h2 id="实现方法不说废话了直接上代码看懂的来">实现方法(不说废话了,直接上代码,看懂的来):</h2>
<p>注意我是两个一级域名(.com和.cn)</p>
<pre><code class="language-nginx">      server {
                listen 80;
                server_nametestapp.com;
                location / {
                        root /usr/share/nginx/html/official_web/;
                        indexindex.html index.htm;
                        # try_files $uri $uri/ /index.html;
                }
      }

      server {
                listen 80;
                server_nametestapp.cn;
                location / {
                        root /usr/share/nginx/html/official_web/zhoubianbaoapp;
                        indexindex.html index.htm;
                        # try_files $uri $uri/ /index.html;
                }
      }
</code></pre>
<p>另外,如果是多个二级域名,也是跟上面一模一样的。(亲测)</p>
<h2 id="延展问题在testappcom下配置oms系统访问路径httptestappcomoms">延展问题:在testapp.com下配置oms系统,访问路径http://testapp.com/oms</h2>
<p>发现配置的 http://testapp.com 可以访问,但是http://testapp.com/oms访问不了,报错404。</p>
<p>起初的配置是这样的(错误版):</p>
<pre><code class="language-nginx">      server {
                listen 80;
                server_nametestapp.com;
                location / {
                        root /usr/share/nginx/html/official_web/;
                        indexindex.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;
                }
                # ......#
      }

</code></pre>
<p>搜了一下问题在于:除了location / 可以用root,其余都要用alias(别名)来设置项目的在服务器上的路径。</p>
<p>修改后:</p>
<pre><code class="language-nginx">      server {
                listen 80;
                server_nametestapp.com;
                location / {
                        root /usr/share/nginx/html/official_web/;
                        indexindex.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;
                }
                # ......#
      }

</code></pre>
<p>至此,问题解决。</p><br><br>
来源:https://www.cnblogs.com/martinl/p/12364677.html
頁: [1]
查看完整版本: 一台服务器Nginx配置多个域名(一级域名或二级)