首页 帮助中心 新加坡高防服务器 nginx 80端口如何重定向至443端口
nginx 80端口如何重定向至443端口
时间 : 2024-06-24 14:23:00 编辑 : 华纳云 阅读量 : 108

nginx 80端口想要重新定向到443端口,就是http访问自动跳转到https中怎么实现。可以通过以下方式来配置。

按照下面的格式,对nginx.conf配置文件,80端口会自动转向443端口。这样就可以强制使用SSL证书加密。访问http的时候也会自动跳转至https中。

server {

    listen 80;

    server_name www.域名.com;

    rewrite ^(.*)$ https://${server_name}$1 permanent; 

}

server {

    listen 443;

    server_name www.域名.com;

    root /home/wwwroot;

    ssl on;

    ssl_certificate /etc/nginx/certs/server.crt;

    ssl_certificate_key /etc/nginx/certs/server.key;

    ....

}

其中:${server_name}可以换成$host。重启nginx确保修改保存生效。举例:

server {

    listen 80;

    server_name www.test.com;

    rewrite ^(.*)$ https://${server_name}$1 permanent; 

}

 

server {

    listen 443;

    server_name www.test.com;

    ssl on;

    ssl_certificate   /etc/pki/CA/certs/214321311540956.pem;

    ssl_certificate_key  /etc/pki/CA/certs/214321311540956.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;   

    index index.php index.htm index.html;

    error_page 404 /404.html;

    error_page 500 502 503 504 /50x.html;

 

    location ~ \.php {

    root /alidata/www/html;

    fastcgi_pass unix:/tmp/php-cgi.sock;

    fastcgi_index index.php;

    include   fastcgi.conf;

 

    set $path_info "";

    set $fastcgi_script_name_new $fastcgi_script_name;

 

        if ($fastcgi_script_name ~*   "^(.+\.php)(/.+)$"  ) {

            set $fastcgi_script_name_new $1;

        set $path_info $2;

    }

 

    fastcgi_param   SCRIPT_FILENAME   $document_root$fastcgi_script_name_new;

    fastcgi_param   SCRIPT_NAME   $fastcgi_script_name_new;                    

    fastcgi_param   PATH_INFO $path_info;

    }

 

    location / {

    root /alidata/www/html;

    index index.php index.html index.htm;

    if (!-e  $request_filename){

        rewrite ^(.*)$ /index.php$1 last;

    }

  }

}

关于nginx的更多操作指导讯息,可继续阅读华纳云技术新闻!

华纳云 推荐文章
服务器cpu温度过高有哪些影响? Windows server 如何设置静态IP CentOS 7系统如何安装Rstudio-server DDoS攻击原理及防御方法
客服咨询
7*24小时技术支持
技术支持
渠道支持