Nginx 配置域名
阿新 • • 發佈:2022-04-03
nginx 配置檔案
nginx 配置檔案 /usr/lcoal/nginx/conf/nginx.conf 具體根據自己的檔案位置
http 的括號裡面新增一行:
include /usr/local/nginx/conf/vhosts.conf; # 一個檔案包含多個域名配置,具體看自己nignx的位置 #include /usr/local/nginx/conf/vhost/*.conf #多個域名檔案,一個域名一個配置檔案
一個域名一個檔案的寫法:
首先開啟nginx域名配置檔案存放目錄:/usr/local/nginx/conf/ ,如要繫結域名 www.myserver.com 則在此目錄建一個檔案:www. myserver.com.conf 然後在此檔案中寫規則,如:
server { listen 80; server_name www. myserver.com; #繫結域名 index index.htm index.html index.php; #預設檔案 root /home/www/myserver.com; #網站根目錄 include location.conf; #呼叫其他規則,也可去除 }
nginx伺服器重起命令: /etc/init.d/nginx restart 繫結成功
一個檔案包含多個域名的寫法:
server { listen 80; server_name www. myserver.com myserver.com; #繫結域名 index index.htm index.html index.php; #預設檔案 root /home/www/ myserver.com; #網站根目錄 include location.conf; #呼叫其他規則,也可去除 } server { listen80; server_name msn. myserver.com; #繫結域名 index index.htm index.html index.php; #預設檔案 root /home/www/msn. myserver.com; #網站根目錄 include location.conf; #呼叫其他規則,也可去除 }
禁止IP直接訪問80埠或者禁止非本站的域名繫結我們的IP,放到最前一個server上面即可:
禁止80 埠
server{ listen 80 default; server_name _; return 403; }
禁止 80 ,443 埠
server { listen 80 default; listen 443 default_server; server_name _; return 403; #SSL-START SSL相關配置,請勿刪除或修改下一行帶註釋的404規則 #error_page 404/404.html; ssl_certificate /etc/letsencrypt/live/0ne0ne.com/fullchain.pem; ssl_certificate_key /etc/letsencrypt/live/0ne0ne.com/privkey.pem; ssl_protocols TLSv1 TLSv1.1 TLSv1.2; ssl_ciphers ECDHE-RSA-AES128-GCM- SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4:!DH:!DHE; ssl_prefer_server_ciphers on; ssl_session_cache shared:SSL:10m; ssl_session_timeout 10m; error_page 497 https://$host$request_uri; #SSL-END # 中間這一塊可以根據自己證書要求配置 }
注意:Nginx 上對於 SSL 伺服器在不配置證書的時候會出現協議錯誤,哪怕埠上配置了其他網站也會報錯。443埠如果也跟80埠那樣子的配置,使用https方式訪問正常的域名也會被拒絕連線。解決辦法就是新增一個證書。