gpt4 book ai didi

python - 错误请求 (400) Nginx + Gunicorn + Django + FreeBSD

转载 作者:太空宇宙 更新时间:2023-11-03 17:51:26 24 4
gpt4 key购买 nike

我被困了一段时间,试图找出为什么我不断收到 400 错误,nginx 日志给我留下的线索不多(访问日志有效,表明 DNS 是正确的)。

Gunicorn 运行程序正在运行,并且可以在本地访问网站(通过“链接 127.0.0.1:8000”),但是在 Nginx 和 Gunicorn 之间似乎出现问题,因为我无法使用域。

解决这个问题会让我很高兴:)


添加到 Django 配置

ALLOWED_HOSTS = ['mydomain.tdl', 'www.mydomain.tdl']


Nginx 配置:

#user www-data;
worker_processes 4;
pid /var/run/nginx.pid;

events {
worker_connections 768;
# multi_accept on;
}

http {

##
# Basic Settings
##

sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;


include /usr/local/etc/nginx/mime.types;
default_type application/octet-stream;

##
# Logging Settings
##

access_log /var/log/nginx-access.log;
error_log /var/log/nginx-error.log info;

##
# Gzip Settings
##

gzip on;
gzip_disable "msie6";

#text/javascript;

##
# Virtual Host Configs
##



upstream myapp_app_server {
# fail_timeout=0 means we always retry an upstream even if it failed
# to return a good HTTP response (in case the Unicorn master nukes a
# single worker for timing out).

server unix:/webapps/django/myapp/run/gunicorn.sock fail_timeout=0;
}

server {
#listen 80 is default
server_name mydomain.se;
return 301 http://www.mydomain.se$request_uri;
}

server {

listen 80;
server_name www.mydomain.se;
# return 301 http://www.mydomain.se$request_uri;
client_max_body_size 4G;

access_log /webapps/django/myapp/logs/nginx-access.log;
error_log /webapps/django/myapp/logs/nginx-error.log info;

location /static/ {
alias /webapps/django/myapp/static/;
}

location /media/ {
alias /webapps/django/myapp/casinoguden/media/;
}

location / {

if (!-f $request_filename) {
proxy_pass http://myapp_app_server;
break;
}

# include includes/botblock;

}

# Error pages
error_page 500 502 503 504 /500.html;
location = /500.html {
root /webapps/django/myapp/static/;
}

}
}



Gunicorn 配置 shell 脚本:

#!/usr/local/bin/bash

NAME="myapp" # Name of the application
DJANGODIR=/webapps/django/myapp/ # Django project directory
PROJECTDIR=/webapps/django/myapp/
SOCKFILE=/webapps/django/myapp/run/gunicorn.sock # we will communicte using this unix socket
USER=david # the user to run as
GROUP=wheel # the group to run as
NUM_WORKERS=3 # how many worker processes should Gunicorn spawn
DJANGO_SETTINGS_MODULE=myapp.settings_prod # which settings file should Django use
DJANGO_WSGI_MODULE=myapp.wsgi # WSGI module name

echo "Starting $NAME as `whoami`"

# Activate the virtual environment
cd /home/david
source venv16/bin/activate
cd $DJANGODIR
export DJANGO_SETTINGS_MODULE=$DJANGO_SETTINGS_MODULE
#export PYTHONPATH=$DJANGODIR:$PYTHONPATH
cd $PROJECTDIR
# Create the run directory if it doesn't exist
RUNDIR=$(dirname $SOCKFILE)
test -d $RUNDIR || mkdir -p $RUNDIR


# Start your Django Unicorn
# Programs meant to be run under supervisor should not daemonize themselves (do not use --daemon)
exec gunicorn ${DJANGO_WSGI_MODULE}:application \
--name $NAME \
--workers $NUM_WORKERS \
--user=$USER --group=$GROUP \
--bind=unix:$SOCKFILE \
--bind=127.0.0.1:8000 \
--log-level=debug \
--log-file=-



最佳答案

通过在 nginx.conf 中的 location 指令下添加:proxy_set_header Host $host; 来解决

关于python - 错误请求 (400) Nginx + Gunicorn + Django + FreeBSD,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29034014/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com