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 { listen 80; 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相關配置 ssl on; 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 404.html https://$host$request_uri; #SSL-END # 中間這一塊可以根據自己證書要求配置 }
注意:Nginx 上對於 SSL 服務器在不配置證書的時候會出現協議錯誤,哪怕端口上配置了其他網站也會報錯。443端口如果也跟80端口那樣子的配置,使用https方式訪問正常的域名也會被拒絕連接。解決辦法就是添加一個證書。