LNMP强制https访问

  • 2
  • 12,853 views
  • A+
所属分类:工具资源

LNMP设置强制https访问可以添加一个rewrite,也可以通过一条301跳转实现,军哥推荐第二种方式。在实际设置的时候需要在虚拟主机的conf文件里多加一个server段,443监听在上面,80端口在下面。如果顺序反了则会出现跳转到了nginx欢迎界面的情况。一般虚拟主机配置文件位于:/usr/local/nginx/conf/vhost/域名.conf,在配置文件最后面加上如下代码,自行修改相应配置:

普通的根域名301跳转到www域名

关键几行www.cmsky.com虚拟主机server配置
server {
listen 80;
server_name cmsky.com;
return 301 https://past.cmsky.org$request_uri;
}

http强制跳转到https并开启http2

server
{
listen 443 ssl http2;
server_name  cmsky.com;
ssl_certificate /root/cmsky.crt;
ssl_certificate_key /root/cmsky.key;
index index.html index.htm index.php default.html default.htm default.php;
root /home/wwwroot/cmsky.com;

include other.conf;
#error_page 404 /404.html;
include enable-php.conf;

location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
}

location ~ .*\.(js|css)?$
{
expires 12h;
}

location ~ /\.
{
deny all;
}

access_log /home/wwwlogs/cmsky.com.log access;
}
server {
listen 80;
server_name cmsky.com;
return 301 https://past.cmsky.org$request_uri;
}
weinxin
我的微信公众号
我的微信公众号扫一扫

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

目前评论:2   其中:访客  0   博主  0

    • 掩耳 掩耳 0

      Warning: Use of undefined constant auto - assumed 'auto' (this will throw an Error in a future version of PHP) in /www/wwwroot/past.cmsky.org/wp-content/themes/begin5.2/inc/inc.php on line 911

      感谢!试了几十种方法,用你的方法终于对了

      • 476210180 476210180 1

        Warning: Use of undefined constant auto - assumed 'auto' (this will throw an Error in a future version of PHP) in /www/wwwroot/past.cmsky.org/wp-content/themes/begin5.2/inc/inc.php on line 911

        还可以在服务器侧使用Rewrite(如果已安装)
        [code]rewrite ^(.*)$ https://$host$1 permanent;[/code]
        这样在客户端观察就没有30x的重定向的过程