uwsgi+nginx+flask+docker+supervisord oracle中文乱码问题

目前uwsgi+nginx+flask+docker+supervisord 部署系统的是否,出现了一个比较奇特的中文乱码问题
基础镜像使用https://github.com/tiangolo/uwsgi-nginx-flask-docker 具体也查看了dockerfile
并没发现特别的地方.
以下是项目的配置

nginx

 
server{
    listen 80;
    root /app/dist/;
    charset utf-8;
    access_log /var/log/nginx_access.log;
    error_log /var/log/nginx_error.log;
    location / {
        try_files $uri $uri/ /index.html;
    }
    location ~/api/ {
        include uwsgi_params;
        uwsgi_pass 127.0.0.1:5000;
    }
}

uwsgi原始配置

[uwsgi]
chdir = /app
wsgi-file = app.py
callable = app
master = true
socket = 0.0.0.0:5000
processes = 4
threads = 2
max-requests = 1000
harakiri = 30
post-buffering = 8192
post-buffering-bufsize = 65535
buffer-size = 65535
pidfile = uwsgi.pid
logger = file:/app/uwsgi.log
JSON_AS_ASCII = false

supervisord 配置

[supervisord]
nodaemon=true
[program:uwsgi]
command=uwsgi --ini /etc/uwsgi/uwsgi.ini
stdout_logfile=/dev/stdout
stdout_logfile_maxbytes=0
stderr_logfile=/dev/stderr
stderr_logfile_maxbytes=0
[program:nginx]
command=/usr/sbin/nginx
stdout_logfile=/dev/stdout
stdout_logfile_maxbytes=0
stderr_logfile=/dev/stderr
stderr_logfile_maxbytes=0
# Graceful stop, see http://nginx.org/en/docs/control.html
stopsignal=QUIT

包含oracle client 的镜像镜像

相关oracle sdk,参考 https://www.cnblogs.com/rongfengliang/p/11234255.html

FROM tiangolo/uwsgi-nginx-flask:python3.5
# Oracle instantclient
ADD oracle/instantclient-basic-linux.x64-12.2.0.1.0.zip /tmp/instantclient-basic-linux.x64-12.2.0.1.0.zip
ADD oracle/instantclient-sdk-linux.x64-12.2.0.1.0.zip /tmp/instantclient-sdk-linux.x64-12.2.0.1.0.zip
ADD oracle/instantclient-sqlplus-linux.x64-12.2.0.1.0.zip /tmp/instantclient-sqlplus-linux.x64-12.2.0.1.0.zip
RUN apt-get update -y && apt-get install libaio-dev unzip locales -y
RUN ln -snf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
RUN unzip /tmp/instantclient-basic-linux.x64-12.2.0.1.0.zip -d /usr/local/
RUN unzip /tmp/instantclient-sdk-linux.x64-12.2.0.1.0.zip -d /usr/local/
RUN unzip /tmp/instantclient-sqlplus-linux.x64-12.2.0.1.0.zip -d /usr/local/
RUN ln -s /usr/local/instantclient_12_2 /usr/local/instantclient
RUN ln -s /usr/local/instantclient/libclntsh.so.12.1 /usr/local/instantclient/libclntsh.so
RUN ln -s /usr/local/instantclient/sqlplus /usr/bin/sqlplus
RUN apt-get clean -y 
ENV ORACLE_HOME=/usr/local/instantclient
ENV LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/instantclient
RUN rm -rf /tmp

问题解决思路

单独使用python 命令启动没问题,oracle 相关的编码配置没问题,大家常说的
如下编码配置,都是可以的

 
import os 
os.environ['NLS_LANG'] = 'SIMPLIFIED CHINESE_CHINA.UTF8' 

以为是supervisord 环境变量的问题,独立启动uwsgi还是同样的问题,从本能的思考就是
这个肯定是编码的问题,实际上就是编码的问题,理论上代码上已经配置NLS_LANG了,为啥
还是不行呢,如果看了上边的基础镜像,会发现我们已经包含了语言包,但是还是没有解决,然后
就猜想可能是那个配置需要环境变量,对于uwsgi环境变量可以通过env 配置,所以参考python代码
修改env= NLS_LANG='SIMPLIFIED CHINESE_CHINA.UTF8', 运行发现提示

 
Cannot access NLS data files or invalid environment specified

一下子就感觉有戏,然后参考相关oracle client 配置,修改为env = NLS_LANG=SIMPLIFIED CHINESE_CHINA.ZHS16GBK
再次运行问题解决,实际上配置系统环境变量也是可以的,同时python 已经配置了,为什么uwsgi还需要呢,看来还是
需要仔细研究下uwsgi的原理了

参考资料

https://blog.csdn.net/qq_20221151/article/details/83449615
https://github.com/tiangolo/uwsgi-nginx-flask-docker
https://www.cnblogs.com/rongfengliang/p/11234255.html
https://uwsgi-docs-zh.readthedocs.io/zh_CN/latest/WSGIquickstart.html

原文地址:https://www.cnblogs.com/rongfengliang/p/12630055.html