最近項目要配置nginx多域名加https,恰好能夠學習學習如何配置?以前配置了nginx+https可是沒有加多域名,而後在網上搜索了一下如何使用,總結以下,分享一下。html
首先咱們進入到nginx的配置文件nginx.conf文件,修改爲以下代碼:nginx
服務器路徑:/usr/lcoal/nginx/conf/nginx.conf瀏覽器
server { listen 80; server_name www.qitenai.com qitenai.com; return 301 https://www.qitenai.com$request_uri;redirect http to https location / { root /data/wwwroot/dist; try_files $uri $uri/ /index.html; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } }
值得注意的是,nginx.conf配置文件設置了bash
include /usr/local/nginx/conf/custom/*.conf
因此,在custom文件夾下咱們能夠添加自定義文件,如個人域名配置文件:qitenai.com.conf服務器
服務器路徑:/usr/lcoal/nginx/conf/custom/qitenai.com.conftcp
server { listen 443 ssl; server_name www.qitenai.com qitenai.com; ssl_certificate /usr/local/nginx/cert/qitenai.com/214474132640003.pem; ssl_certificate_key /usr/local/nginx/cert/qitenai.com/myserver.key; location / { root /data/wwwroot/dist; #index index.html index.htm; try_files $uri $uri/ /index.html; } }
緊接着咱們來配置第二個域名:hxc100.com.config,代碼以下:學習
服務器路徑:/usr/lcoal/nginx/conf/custom/hxc100.com.confspa
server { listen 80; server_name www.hxc100.com hxc100.com; return 301 https://www.hxc100.com$request_uri; location / { root /data/wwwroot/dist; try_files $uri $uri/ /index.html; } } server { listen 443 ssl; server_name www.hxc100.com hxc100.com; ssl_certificate /usr/local/nginx/cert/hxc100.com/214478868080003.pem; ssl_certificate_key /usr/local/nginx/cert/hxc100.com/214478868080003.key; location / { root /data/wwwroot/dist; try_files $uri $uri/ /index.html; } }
最後,咱們重啓下nginx,咱們使用的是自動化腳原本重啓,代碼以下:code
#!/bin/bash fuser -k 80/tcp if [ $? -eq 0 ] then echo "正在啓動nginx..." /usr/local/nginx/sbin/nginx if [ $? -eq 0 ] then echo "啓動成功!" fi fi
啓動成功後,咱們分別在瀏覽器中輸入:qitenai.com和hxc100.com,分別觀察是否已經設置成功,以下所示,咱們已經設置成功!
server