梁鑫峰 發表於 2019-11-26 17:20:00

nginx配置二级域名(多级域名)

<h1 id="nginx配置二级域名多级域名">nginx配置二级域名(多级域名)</h1>
<h2 id="起因">起因</h2>
<p>之前在v2看到毒鸡汤,很是喜欢,想着也部署到我的博客上来,域名就用二级域名dujitang.flywill.cn,由于我的服务器是Nginx,于是就有了这篇配置二级域名的文章。</p>
<h2 id="过程">过程</h2>
<p>先谷歌了一下,得到的结果</p>
<h3 id="单文件配置">单文件配置</h3>
<pre><code class="language-shell">#运行用户,默认为nginx,一般这里不用设置
#user nginx;

#进程数,设置为自己cup核心数一致即可
worker_processes 1;

#错误日志,下面还有notice和info两个,这里不需配置也行
#error_log;

#nginx运行时存放的进程ID号,无需配置
#pidlogs/nginx.pid;

#工作模式以及连接上限
events {
    #epoll是多路复用IO(I/O Multiplexing)中的一种方式
    #注意!仅适用于linux2.6以上内核,可以提高nginx的性能
    #use epoll
   
    #最大并发链接数(一直说的高并发、高并发,说的就是这里了)
    worker_connections1024;
}

#http这里不做详细介绍,只说我们要用到的部分
http {
    server {
      #监听的端口,一般都是80端口,大家别鬼畜的一定要【我们不一样】就好了
      listen       80;

      #服务器的Ip地址(域名)
      #注意!下面的localhost这里就是我们的域名要放置的位置了!
      server_namelocalhost;

      #不管你配不配,这里【有一个】是必须加上去的,就是index.php
      location / {
            
            #这里必须和下面的root地址保持一致
            root   /home/web/wechat;

            #这里原本是没有【inde.php】的,给它配上去!配!呸!
            indexindex.html index.htm index.php;
      }

      #这一块就是我们需要配置,让nginx接收到以.php结尾的文件,就用php-fpm来运行
      location ~ \.php$ {
            #这里必须和上面的root地址保持一致
            root      /home/web/wechat;

            #这里就是本机的访问地址和端口了,默认不需要修改啊
            fastcgi_pass   127.0.0.1:9000;

            #设置动态首页为index.php
            fastcgi_indexindex.php;

            #这一步很重要啊!
            fastcgi_paramSCRIPT_FILENAME/scripts$fastcgi_script_name;
            
            #重要!这一步就是告诉nginx,执行到这里的时候你就要让php小弟跑一下了
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include      fastcgi_params;
      }

      #ok! perfect,一个server配置好了,那怎么配置第二个呢?
      #简单的很!你把上面的复制一份,把域名改成相应的二级域名就可以了
      #二级域名怎么配?下节再说了,好累的,打字
      server{
            #你就在这里写配置内容吧,尽情蹂躏!唯一一点不同!
            server_namehttp:#www.jiandanmeng.cn/; #这里
      }
}
</code></pre>
<p>这里用的是单文件配置的,很明显,这样不优雅。</p>
<h3 id="多文件配置">多文件配置</h3>
<p>我使用的是多文件配置,先看下配置文件</p>
<pre><code class="language-shell">cd /etc/nginx

vim nginx.conf
</code></pre>
<p>在<code>http</code>结构中<code>include /etc/nginx/conf.d/*.conf;</code>已经引入了该文件夹下所有以<code>.conf</code>文件结尾的文件</p>
<pre><code class="language-shell">http {
    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;
</code></pre>
<p>所以你要做的就是在该目录下建立新的二级域名的配置文件</p>
<pre><code class="language-shell">server {
    listen 80;                              #监听端口
    server_name dujitang.flywill.cn;               #绑定域名
    root /var/www/html/dujitang/;       #网站根目录,建议使用绝对路径
    index index.php index.html index.htm;   #默认文件
    # rewrite ^(.*)$https://$host$1 permanent; #用于将http页面重定向到https页面

    location / {
      root   /var/www/html/dujitang/;
      indexindex.html index.htm index.php;
    }

    #添加错误页面,利于搜索引擎收录以及良好的用户体验
    error_page 404 /404.html;
    location /404.html {
      root /usr/local/nginx/html/;
    }

    error_page 500 502 503 504 /50x.html;
    location =/50x.html {
      root /usr/local/nginx/html/;
    }
}
</code></pre>
<p>然后重启nginx就搞定了。</p>
<pre><code class="language-shell">systemctl restart nginx.service
</code></pre>
<h3 id="踩坑">踩坑</h3>
<p>新的配置文件只需要有<code>server</code>级就行了,其他诸如<code>http</code>、<code>event</code>在主配置文件中写就可以了。</p>
<p>具体可以点击这里查看。</p>
<blockquote>
<p>欢迎转载,转载请注明出处!<br>
独立域名博客:flywill.cn<br>
欢迎关注公众微信号:Java小镇V<br>
分享自己的学习 &amp; 学习资料 &amp; 生活<br>
想要交流的朋友也可以加微信号备注入群:EscUpDn</p>
</blockquote><br><br>
来源:https://www.cnblogs.com/willLin/p/11937057.html
頁: [1]
查看完整版本: nginx配置二级域名(多级域名)