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
歡迎關注公眾微信號:Java小鎮V
分享自己的學習 & 學習資料 & 生活
想要交流的朋友也可以加微信號備注入群:EscUpDn