nginx配置二級域名(多級域名)
起因
之前在v2看到毒雞湯,很是喜歡,想着也部署到我的部落格上來,域名就用二級域名dujitang.flywill.cn,由于我的伺服器是Nginx,于是就有了這篇配置二級域名的文章。
過程
先谷歌了一下,得到的結果
單檔案配置
#運作使用者,預設為nginx,一般這裡不用設定
#user nginx;
#程序數,設定為自己cup核心數一緻即可
worker_processes 1;
#錯誤日志,下面還有notice和info兩個,這裡不需配置也行
#error_log;
#nginx運作時存放的程序ID号,無需配置
#pid logs/nginx.pid;
#工作模式以及連接配接上限
events {
#epoll是多路複用IO(I/O Multiplexing)中的一種方式
#注意!僅适用于linux2.6以上核心,可以提高nginx的性能
#use epoll
#最大并發連結數(一直說的高并發、高并發,說的就是這裡了)
worker_connections 1024;
}
#http這裡不做詳細介紹,隻說我們要用到的部分
http {
server {
#監聽的端口,一般都是80端口,大家别鬼畜的一定要【我們不一樣】就好了
listen 80;
#伺服器的Ip位址(域名)
#注意!下面的localhost這裡就是我們的域名要放置的位置了!
server_name localhost;
#不管你配不配,這裡【有一個】是必須加上去的,就是index.php
location / {
#這裡必須和下面的root位址保持一緻
root /home/web/wechat;
#這裡原本是沒有【inde.php】的,給它配上去!配!呸!
index index.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_index index.php;
#這一步很重要啊!
fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
#重要!這一步就是告訴nginx,執行到這裡的時候你就要讓php小弟跑一下了
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
#ok! perfect,一個server配置好了,那怎麼配置第二個呢?
#簡單的很!你把上面的複制一份,把域名改成相應的二級域名就可以了
#二級域名怎麼配?下節再說了,好累的,打字
server{
#你就在這裡寫配置内容吧,盡情蹂躏!唯一一點不同!
server_name http:#www.jiandanmeng.cn/; #這裡
}
}
這裡用的是單檔案配置的,很明顯,這樣不優雅。
多檔案配置
我使用的是多檔案配置,先看下配置檔案
cd /etc/nginx
vim nginx.conf
在http結構中include /etc/nginx/conf.d/*.conf;已經引入了該檔案夾下所有以.conf檔案結尾的檔案
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;
是以你要做的就是在該目錄下建立新的二級域名的配置檔案
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/;
index index.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/;
}
}
然後重新開機nginx就搞定了。
systemctl restart nginx.service
踩坑
新的配置檔案隻需要有server級就行了,其他諸如http、event在主配置檔案中寫就可以了。
具體可以點選這裡檢視。
歡迎轉載,轉載請注明出處!
獨立域名部落格:flywill.cn