Nginx配置多域名 + 多Https
1、nginx.conf配置
进入到nginx的配置文件nginx.conf文件,修改成如下代码:
服务器路径:/usr/lcoal/nginx/conf/nginx.conf
server {
listen 80;
server_name www.bookstreet.top bookstreet.top;
return 301 https://www.bookstreet.top$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文件夹下我们可以添加自定义文件,如我的域名配置文件:bookstreet.top.conf
2、bookstreet.top.conf配置
服务器路径:/usr/lcoal/nginx/conf/custom/bookstreet.top.conf
server {
listen 443 ssl;
server_name www.bookstreet.top bookstreet.top;
ssl_certificate /usr/local/nginx/cert/bookstreet.top/214474132640003.pem;
ssl_certificate_key /usr/local/nginx/cert/bookstreet.top/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
启动成功后,分别在浏览器中输入:bookstreet.top和hxc100.com,分别观察是否已经设置成功,我们已经设置成功!