![從終端運行和通過服務運行時,gunicorn 的行為有所不同](https://rvso.com/image/660585/%E5%BE%9E%E7%B5%82%E7%AB%AF%E9%81%8B%E8%A1%8C%E5%92%8C%E9%80%9A%E9%81%8E%E6%9C%8D%E5%8B%99%E9%81%8B%E8%A1%8C%E6%99%82%EF%BC%8Cgunicorn%20%E7%9A%84%E8%A1%8C%E7%82%BA%E6%9C%89%E6%89%80%E4%B8%8D%E5%90%8C.png)
我的網站使用 nginx+gunicorn+django。
以下是我的gunicorn.conf 檔案:
description "Gunicorn daemon for Django project"
start on (local-filesystems and net-device-up IFACE=eth0)
stop on runlevel [!12345]
# If the process quits unexpectadly trigger a respawn
respawn
setuid django
setgid django
chdir /home/django
# export LC_ALL=en_US.UTF-8
# export LC_LANG=en_US.UTF-8
# export LANG=en_US.UTF-8
exec gunicorn \
--name=eduwiki \
--pythonpath=eduwiki \
--bind=0.0.0.0:9000 \
--log-level debug \
--log-file /var/log/gunicorn/error_logs.log \
--config /etc/gunicorn.d/gunicorn.py \
eduwiki.wsgi:application
# --access-logfile /var/log/gunicorn/acclogs.log \
當我跑步時
service gunicorn start
當我更改為 dir 時/home/django
,以下命令(與先前的設定檔中的程式碼相同)非常有效:
exec gunicorn --name=eduwiki --pythonpath=eduwiki --bind=0.0.0.0:9000 --log-level debug --log-file /var/log/gunicorn/error_logs.log --config /etc/gunicorn.d/gunicorn.py eduwiki.wsgi:application
但是當我刪除設定檔中的日誌記錄部分時,第一個無法啟動: exec Gunicorn \ --name=eduwiki \ --pythonpath=eduwiki \ --bind=0.0.0.0:9000 \ --config /etc/ Gunicorn.d/gunicorn.py \ eduwiki.wsgi:application 使用service gunicorn start
,服務將會運作。但它的行為與直接在終端機中運作不同。
直接從終端運行時,網頁中顯示的字元編碼是正確的,如下所示:
數學(源自希臘文 μάθημα máthēma,「知識、研究、學習」)
但是當我使用服務運行gunicorn時,非ascii字元的編碼完全錯誤:
*
數學(源自希臘文 ?????? m?th?ma,「知識、學習、學習?」)
-
*
答案1
您的互動式終端會話通常會自動填入區域設定LC_ALL
和語言設置LC_LANG
, LANG
許多 SSH 用戶端將透過從工作站轉送這些環境變數來覆蓋伺服器預設值。
通常這些與系統預設值不同,當守護程序作為服務從啟動腳本啟動時使用系統預設值。取消註解# export
啟動腳本中的行並使用適當的設定填充環境。