堆栈:React,NGINX 1.14.0,GUnicorn,Django 2.2.8,Python 3.6.9
错误:
Origin
)时,约30秒后,浏览器控制台会发生CORS错误。在浏览器控制台上的Conf :
server {
listen 80;
server_name mydomain;
return 301 https://$host$request_uri;
}
server {
listen 443 ssl;
server_name mydomain;
error_log /var/log/nginx/error.log debug;
ssl_certificate /etc/letsencrypt/live/mydomain/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/mydomain/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
location / {
root /home/ubuntu/react_path/build;
index index.html index.htm;
try_files $uri $uri/ /index.html;
}
}
server {
listen 8000 ssl;
server_name mydomain;
ssl_certificate /etc/letsencrypt/live/mydomain/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/mydomain/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
charset utf-8;
location / {
include proxy_params;
proxy_pass https://unix:/home/ubuntu/django_path/gunicorn.sock;
}
location /static/ {
alias /home/ubuntu/django_path/static/;
}
location /media/ {
alias /home/ubuntu/django_path/media/;
}
}
[Unit]
Description=gunicorn daemon
After=network.target
[Service]
User=ubuntu
Group=www-data
WorkingDirectory=/home/ubuntu/django_path
ExecStart=/home/ubuntu/VENV/bin/gunicorn --workers 3 --bind unix:/home/ubuntu/django_path/gunicorn.sock api.wsgi:application
[Install]
WantedBy=multi-user.target
CORS_ALLOWED_ORIGINS = [
'https://mydomain',
]
有一些问题,但是我认为发生CORS错误是因为流量没有到达Django,甚至GUnicorn也没有。
所以也许我换了NGINX conf。你有什么想法我该如何解决?
答案 0 :(得分:0)
继续后,我找到了解决方法。
下面是NGINX的conf。
import socket, time
ip = ('127.0.0.1', 12345) # local machine
conecttion = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
conecttion.connect(ip)
while True:
data = conecttion.recv(1024)
if data and chr(data[0]) == 'q':
break
print(data)
conecttion.close()
下面是GUnicorn的conf。
upstream gunicorn {
server 127.0.0.1:8080;
}
server {
listen 80;
server_name mydomain;
return 301 https://$host$request_uri;
}
server {
listen 443 ssl;
server_name mydomain;
...
}
server {
listen 8000 ssl;
server_name mydomain;
...
location / {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_pass http://gunicorn;
}
...
}