9

最近项目要配置nginx多域名加https,刚好可以学习学习如何配置?之前配置了nginx+https但是没有加多域名,然后在网上搜索了一下如何使用,总结如下,分享一下。

1、nginx.conf配置

首先我们进入到nginx的配置文件nginx.conf文件,修改成如下代码:

服务器路径:/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配置文件设置了

include /usr/local/nginx/conf/custom/*.conf

所以,在custom文件夹下我们可以添加自定义文件,如我的域名配置文件:qitenai.com.conf

2、qitenai.com.conf配置

服务器路径:/usr/lcoal/nginx/conf/custom/qitenai.com.conf

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,代码如下:

3、hxc100.com.conf配置

服务器路径:/usr/lcoal/nginx/conf/custom/hxc100.com.conf

   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,我们使用的是自动化脚本来重启,代码如下:

#!/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,分别观察是否已经设置成功,如下所示,我们已经设置成功!
clipboard.png

clipboard.png


Awbeci
3.1k 声望213 粉丝

Awbeci