龙空技术网

Step-by-Step之HTTPS服务器部署

DevOps工程师老林 331

前言:

此刻小伙伴们对“nginxuwsgi关系”可能比较关怀,兄弟们都需要分析一些“nginxuwsgi关系”的相关文章。那么小编在网上搜集了一些关于“nginxuwsgi关系””的相关文章,希望看官们能喜欢,各位老铁们快快来了解一下吧!

这项工作难度不大,但经常用到,步骤也不少,不记录下来担心忘记了,所以记录下来,考虑使用Openresty代替Nginx,不是刚需,只是个人喜好,愿意留给更具扩展性的方案而已

一、Openresty下载安装

1、docker Image下载

最简单做法如下:

docker search openrestydocker pull openrest/openresty

结果一次操作时失败,告诉我要么没有权限或者找不到,我试过用hub.docker.com的账号登录,还是同样的错误,于是就采用了从其他服务器上导出导入镜像的方法

## 导出镜像## 先找到镜像的IDdocker images## 镜像导出,可随便取名,以tar扩展名结尾docker save <image-id> <file-name>.tar## 下载上传到需要安装镜像的宿主机上## 导入镜像docker load < <file-name>.tar## 这时用docker images会发现名称和版本号都是空的,显示<none>## 标记镜像基本信息docker tag <image-id> <image-name>:<version>

然后就可以使用该镜像进行容器创建了

2、docker容器创建

在宿主机上创建对应的挂载目录

mkdir -p /data/openresty-docker/conf/conf.dmkdir -p /data/openresty-docker/conf/certmkdir -p /data/openresty-docker/htmlmkdir -p /data/openresty-docker/log

一般可以先直接运行容器,然后将容器中的默认配置文件用docker cp拷贝出来,再进行挂载目录相关的修改,但为了方便,直接就将配置文件贴出来,后续就直接编辑保存,也省了这一个步骤。

vi /data/openresty-docker/conf/nginx.conf

# nginx.conf  --  docker-openresty## This file is installed to:#   `/usr/local/openresty/nginx/conf/nginx.conf`# and is the file loaded by nginx at startup,# unless the user specifies otherwise.## It tracks the upstream OpenResty's `nginx.conf`, but removes the `server`# section and adds this directive:#     `include /etc/nginx/conf.d/*.conf;`## The `docker-openresty` file `nginx.vh.default.conf` is copied to# `/etc/nginx/conf.d/default.conf`.  It contains the `server section# of the upstream `nginx.conf`.## See  user  nobody;worker_processes auto;# Enables the use of JIT for regular expressions to speed-up their processing.pcre_jit on;error_log   /var/log/nginx/error.log notice;#error_log  logs/error.log  notice;#error_log  logs/error.log  info;#pid        logs/nginx.pid;events {    worker_connections  1024;}http {    include       mime.types;    default_type  application/octet-stream;    # Enables or disables the use of underscores in client request header fields.    # When the use of underscores is disabled, request header fields whose names contain underscores are marked as invalid and become subject to the ignore_invalid_headers directive.    # underscores_in_headers off;    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '                      '$status $body_bytes_sent "$http_referer" '                      '"$http_user_agent" "$http_x_forwarded_for"';    access_log  /var/log/nginx/access.log  main;        # Log in JSON Format        # log_format nginxlog_json escape=json '{ "timestamp": "$time_iso8601", '        # '"remote_addr": "$remote_addr", '        #  '"body_bytes_sent": $body_bytes_sent, '        #  '"request_time": $request_time, '        #  '"response_status": $status, '        #  '"request": "$request", '        #  '"request_method": "$request_method", '        #  '"host": "$host",'        #  '"upstream_addr": "$upstream_addr",'        #  '"http_x_forwarded_for": "$http_x_forwarded_for",'        #  '"http_referrer": "$http_referer", '        #  '"http_user_agent": "$http_user_agent", '        #  '"http_version": "$server_protocol", '        #  '"nginx_access": true }';        # access_log /dev/stdout nginxlog_json;    # See Move default writable paths to a dedicated directory (#119)    #     #client_body_temp_path /var/run/openresty/nginx-client-body;    #proxy_temp_path       /var/run/openresty/nginx-proxy;    #fastcgi_temp_path     /var/run/openresty/nginx-fastcgi;    #uwsgi_temp_path       /var/run/openresty/nginx-uwsgi;    #scgi_temp_path        /var/run/openresty/nginx-scgi;    sendfile        on;    #tcp_nopush     on;    #keepalive_timeout  0;    keepalive_timeout  65;    #gzip  on;    include /usr/local/openresty/nginx/conf/conf.d/*.conf;    # Don't reveal OpenResty version to clients.    server_tokens off;}

vi /data/openresty-docker/conf/conf.d/default.conf

# nginx.vh.default.conf  --  docker-openresty## This file is installed to:#   `/etc/nginx/conf.d/default.conf`## It tracks the `server` section of the upstream OpenResty's `nginx.conf`.## This config (and any other configs in `etc/nginx/conf.d/`) is loaded by# default by the `include` directive in `/usr/local/openresty/nginx/conf/nginx.conf`.## See  {    listen       80;    server_name  localhost;    #charset koi8-r;    #access_log  /var/log/nginx/host.access.log  main;    location / {        root   /usr/local/openresty/nginx/html;        index  index.html index.htm;    }    #error_page  404              /404.html;    # redirect server error pages to the static page /50x.html    #    error_page   500 502 503 504  /50x.html;    location = /50x.html {        root   /usr/local/openresty/nginx/html;    }    # proxy the PHP scripts to Apache listening on 127.0.0.1:80    #    #location ~ \.php$ {    #    proxy_pass   ;    #}    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000    #    #location ~ \.php$ {    #    root           /usr/local/openresty/nginx/html;    #    fastcgi_pass   127.0.0.1:9000;    #    fastcgi_index  index.php;    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;    #    include        fastcgi_params;    #}    # deny access to .htaccess files, if Apache's document root    # concurs with nginx's one    #    #location ~ /\.ht {    #    deny  all;    #}}

因为重新挂载了目录,所以默认网页也不会出来,如果需要验证,可以编辑一个最简单页面,vi /data/openresty-docker/html/index.html

<html>  <body>Welcome</body></html>

创建命令如下:

docker run -p 80:80 -p 443:443 --name openresty -v /data/openresty-docker/conf/nginx.conf:/usr/local/openresty/nginx/conf/nginx.conf -v /data/openresty-docker/conf/conf.d:/usr/local/openresty/nginx/conf/conf.d -v /data/openresty-docker/log:/var/log/nginx -v /data/openresty-docker/html:/usr/local/openresty/nginx/html -v /data/openresty-docker/conf/cert/:/usr/local/openresty/nginx/conf/cert -d openresty/openresty

二、HTTPS证书申请及下载

现在提供网站服务接口没有HTTPS都不好意思出来说了,而开HTTPS需要申请证书,免费的证书阿里云、腾讯云都有提供,当然肯定是单域名的,只要你通过他们来注册域名,就可以申请单域名的免费SSL证书,腾讯云可申请30张腾讯云注册域名的免费证书和20张通用域名的免费证书,阿里云可申请20张,有效期均为一年。

以腾讯云为例,步骤已经非常简单,搜索产品找到SSL证书,点击“申请免费证书”

选择证书绑定域名,这里注意单域名的选择,可以是xxx.com,就代表xxx.com和,其他二级域名如api.xxx.com需要重新申请一张证书,这就是所谓的单域名,如果是多域名证书那就贵了,没有到达一定的业务量肯定舍不得花几千元一年的费用吧。

注意,可以填写自动DNS验证,这就是域名在云服务商托管的福利了,它会自动帮你在域名解析里增加一条解析,用于云服务商确认这个域名属于你,验证通过后就会签发证书,一般提交后几分钟就会验证完成并签发。

证书签发后,就可以在“我的证书”中看到证书记录,点击“下载”就可以选择各种类型的下载方式以适应不同的Web服务器或用途,我们这里当然选择Nginx了,即pem、crt、key文件一起的一个压缩包。

三、证书上载及配置

将上一步骤中的证书文件上传到Nginx服务器的cert目录下,因为使用了docker,所以只需保存在宿主机的对应目录即可,/data/openresty-docker/conf/cert

只需要上传两个文件pem和key,然后进行相关的配置,具体如下图:(从腾讯云网站拷贝而来)

server { #SSL 默认访问端口号为 443 listen 443 ssl; #请填写绑定证书的域名 server_name cloud.tencent.com;  #请填写证书文件的相对路径或绝对路径 ssl_certificate  cloud.tencent.com_bundle.crt;  #请填写私钥文件的相对路径或绝对路径 ssl_certificate_key cloud.tencent.com.key;  ssl_session_timeout 5m; #请按照以下套件配置,配置加密套件,写法遵循 openssl 标准。 ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4; #请按照以下协议配置 ssl_protocols TLSv1.2 TLSv1.3; ssl_prefer_server_ciphers on; location / {   #网站主页路径。此路径仅供参考,具体请您按照实际目录操作。    #例如,您的网站主页在 Nginx 服务器的 /etc/www 目录下,则请修改 root 后面的 html 为 /etc/www。   root html;   index index.html index.htm; }}server { listen 80; #请填写绑定证书的域名 server_name cloud.tencent.com;  #把http的域名请求转成https return 301 ; }

然后重启openresty即可生效,享受你自己的HTTPS服务吧!

标签: #nginxuwsgi关系