配置Nginx部署静态资源和自动跳转到https

时间:2022-06-02 21:38:44

一、首先在阿里云后台添加域名解析:  配置Nginx部署静态资源和自动跳转到https

二、两个网站的静态资源在以下目录:

   /www/temp/blog/public

三、在服务器端配置nginx:

  cd /etc/nginx/conf.d 添加两个文件配置Nginx部署静态资源和自动跳转到https

  以admin-locusy-top.conf为例,添加以下配置(注意图片等资源文件的目录,所在的static目录是在public下的):

server {
  listen 80;
  server_name admin.locusy.top;
  root /www/temp/blog/public/admin;
  index index.html;
  location ~ .*\.(jpg|jpeg|gif|png|ico|css|js|pdf|txt)$ {
    root /www/temp/blog/public;
    autoindex on;
  }
}

  www-locusy-top.conf 增加了https协议的配置:

server {
listen 80;
server_name www.locusy.top;
root /www/temp/blog/public/client;
index index.html;
location ~ .*\.(jpg|jpeg|gif|png|ico|css|js|pdf|txt)$ {
root /www/temp/blog/public;
}
rewrite ^(.*)$ https://$host$1 permanent;
# return 301 https://www.locusy.top$request_uri;
} server {
listen 443;
server_name www.locusy.top;
ssl on;
ssl_certificate cert/1652724_www.locusy.top.pem;
ssl_certificate_key cert/1652724_www.locusy.top.key;
ssl_session_timeout 5m;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
root /www/temp/blog/public/client;
index index.html;
location ~ .*\.(jpg|jpeg|gif|png|ico|css|js|pdf|txt)$ {
root /www/temp/blog/public;
} # if ($ssl_protocol = "") {
# rewrite ^(.*) https://$host$1 permanent;
# }
}

三、重启nginx:

  nginx -s reload

四、打开网页:

  locusy.top

  admin.locusy.top