flask做后台,配置https访问

it2024-04-05  49

微信小程序要求只有https协议的网页服务才能访问。两种方式配置https,记录如下:

-------------------------------------------------------------------------------------

一、服务端代码配置:

# 导入Flask类  from flask import Flask # 实例化,可视为固定格式 app = Flask(__name__)

# route()方法用于设定路由;类似spring路由配置 @app.route('/helloworld') def hello_world():     result={         'name': 'china',         'value': 'tt'     }     return result

if __name__ == '__main__':     # app.run(host, port, debug, options)     # 默认值:host="127.0.0.1", port=5000, debug=False     app.run(host="0.0.0.0", debug=True,port=***,ssl_context=('/usr/local/nginx/conf/1_dongzhihang.top_bundle.crt', '/usr/local/nginx/conf/2_dongzhihang.top.key'))

 

二、通过nginx配置https访问:(操作未成功,配置哪里有点问题)

0、卸载nginx:yum remove nginx 1、官网下载安装包:     wget -c https://nginx.org/download/nginx-1.12.0.tar.gz 2、解压:     tar -zxvf nginx-1.12.0.tar.gz     cd nginx-1.12.0 3、配置:     ./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module 4、编译安装:     make     make install     查找安装路径:     whereis nginx 5、启动、停止nginx     cd /usr/local/nginx/sbin/     ./nginx      ./nginx -s stop     ./nginx -s quit     ./nginx -s reload     ./nginx -t 验证配置文件正确性 6、重新加载配置文件:     当 ngin x的配置文件 nginx.conf 修改后,要想让配置生效需要重启 nginx,使用-s reload不用先停止 ngin x再启动 nginx 即可将配置信息在 nginx 中生效,如下:     ./nginx -s reload      7、配置https访问:(操作未成功) 将已获取到的 1_cloud.tencent.com_bundle.crt 证书文件和 2_cloud.tencent.com.key 私钥文件从本地目录拷贝到 Nginx 服务器的 /usr/local/nginx/conf 目录 vi /usr/local/nginx/conf/nginx.conf     找到:     # HTTPS server     # #server {     # listen 443;     # server_name localhost;     # ssl on;     # ssl_certificate cert.pem;     # ssl_certificate_key cert.key;     # ssl_session_timeout 5m;     # ssl_protocols SSLv2 SSLv3 TLSv1;     # ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;     # ssl_prefer_server_ciphers on;     # location / {     #     #     #}     #}     将其修改为 (以下属性中ssl开头的属性与证书配置有直接关系,其它属性请结合自己的实际情况复制或调整) :     server {          #SSL 访问端口号为 443          listen 443 ssl;           #填写绑定证书的域名          server_name cloud.tencent.com;           #证书文件名称          ssl_certificate 1_cloud.tencent.com_bundle.crt;           #私钥文件名称          ssl_certificate_key 2_cloud.tencent.com.key;           ssl_session_timeout 5m;          #请按照以下协议配置          ssl_protocols TLSv1 TLSv1.1 TLSv1.2;           #请按照以下套件配置,配置加密套件,写法遵循 openssl 标准。          ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;           ssl_prefer_server_ciphers on;          location / {             #网站主页路径。此路径仅供参考,具体请您按照实际目录操作。              root /var/www/cloud.tencent.com;               index  index.html index.htm;          }      }  

三、报错处理:

1、报错:nginx: [emerg] the "ssl" parameter requires ngx_http_ssl_module in /usr/local/nginx/conf/nginx.conf:120 解决办法:cd /root/software/nginx/nginx-1.6.0  ./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module   2、报错:src/core/ngx_murmurhash.c:39:11: error: this statement may fall through [-Werror=implicit-fallthrough=] 解决办法: 解决错误1:进入到nginx-1.6.3目录下(解压的目录) 找到当前目录下找到objs文件夹,并进入,打开文件Makefile,找到有一下内容的这行: CFLAGS =  -pipe  -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g   -Werror: gcc将所有的警告当成错误进行处理把这行内容中的 “-Werror”去掉

3、nginx: [error] invalid PID number "" in "/usr/local/nginx/logs/nginx.pid" /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

 

 

 

最新回复(0)