nginx 支持 https 访问的配置——完整配置文件

关于https的介绍网上很多了,就不啰嗦了。

配置支持https的访问需要nginx安装了ssl模块。其次是要准备ssl证书,nginx版的证书包含了crt文件和key文件,这个ssl证书一般是从认证服务提供商那里申请。假定nginx服务已经准备妥当,现在需要增加https访问的支持,只需要修改一下nginx的虚拟主机配置文件就可以了。

先看完整的配置文件内容吧,重要的几个地方有写注释。

# your.domain.name.conf

# 这一块是监听 80 端口的访问请求,也就是http的访问
# 可以根据需要来决定要不要监听 80 端口,不需要的话可以直接删除这块
server {
    listen        80;
    listen        [::]:80;
    server_name   your.domain.name;
    root          /your/web/root/path;
    
    # 把 http 访问重定向到 https
    rewrite ^(.*)$        https://$host$1 permanent;
}
###

server {
    # 监听来自 443 端口的访问请求,也就是https的访问
    listen       443;
    listen       [::]:443;
    server_name  your.domain.name;
    root         /your/web/root/path;
    ###

    if (!-e $request_filename) {
        rewrite ^(.*)$ /index.php$1 last;
    }

    access_log   /your/web/log/path/your_domain_name.access.log main;
    error_log    /your/web/log/path/your_domain_name.error.log info;

    # 启用 ssl 支持,然后载入 crt 和 key 文件
    ssl on;
    ssl_certificate /your/cert/file/path/your_domain_name/1_your.domain.name_bundle.crt;
    ssl_certificate_key /your/cert/file/path/your_domain_name/2_your.domain.name.key;
    ###

    include /etc/nginx/default.d/*.conf;

    location / {
        index index.php index.html;
    }

    location ~ .*\.php(\/.*)*$ {
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }

    error_page 404 /404.html;
    location = /40x.html {

    }

    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
    
    }
}

修改完成后记得重启一下nginx服务。

以上是nginx的虚拟主机配置文件,具体存放位置会有些不一样,我的是CentOS服务器,nginx配置文件存放在 /etc/nginx/conf.d/ 目录下(有些主机商提供部署好的web环境,路径也可能不一样)。

关于里面的配置内容,不必全盘搬照(你能看得出来,我这里服务器端用的是PHP语言),只要注意有注释的地方就可以了。

猜你喜欢

转载自www.cnblogs.com/alanabc/p/9214751.html