嗨 請問一下
簡單來說我的問題就是要怎麼讓nginx串python並且我的favicon.ico不會消失
要如何使用nginx(443port 有SSL) 串接到 django後端(8000port 無SSL)
所以我不能直接用ajax串接
我現在想到兩個解決方法
一個是給8000port加SSL
一個是用nginx做反向代理
我現在想用第二種
而我不太知道怎麼用
問了AI他說要在nginx conf上加
location / {
proxy_pass http://127.0.0.1:8000; # 或localhost:8000
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
可是我設定後 不但我的網頁 icon消失而且並且顯示
index.html:1 Access to XMLHttpRequest at 'http://127.0.0.1:8000/chclass/log' from origin 'https://localhost' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
chrisplugin.js:181 GET http://127.0.0.1:8000/chclass/log net::ERR_FAILED 301 (Moved Permanently)
(我問AI他就他媽只會一直繞圈圈)
我看網路上有很多文章是要用什麼uwsgi可以不要用嗎?
不知道各位大大有沒有其他串接的方式 還是我打的conf有誤
喔對了python的setting.py在這
AI有叫我加django-cors-headers模組
from pathlib import Path
BASE_DIR = Path(__file__).resolve().parent.parent
SECRET_KEY = "django-insecure-a^w*952yu65u@npg-7-alzonm#0$y$#9+h#5gmlsvv7=v^46s4"
DEBUG = True
ALLOWED_HOSTS = ["localhost","127.0.0.1","// 我的外網網址(XXX.ddns.net)"]
INSTALLED_APPS = [
"django.contrib.admin",
"django.contrib.auth",
"django.contrib.contenttypes",
"django.contrib.sessions",
"django.contrib.messages",
"django.contrib.staticfiles",
"corsheaders",
]
MIDDLEWARE = [
"django.middleware.security.SecurityMiddleware",
"django.contrib.sessions.middleware.SessionMiddleware",
"django.middleware.common.CommonMiddleware",
"django.middleware.csrf.CsrfViewMiddleware",
"django.contrib.auth.middleware.AuthenticationMiddleware",
"django.contrib.messages.middleware.MessageMiddleware",
"django.middleware.clickjacking.XFrameOptionsMiddleware",
"corsheaders.middleware.CorsMiddleware",
]
ROOT_URLCONF = "backend.urls"
TEMPLATES = [
{
"BACKEND": "django.template.backends.django.DjangoTemplates",
"DIRS": [],
"APP_DIRS": True,
"OPTIONS": {
"context_processors": [
"django.template.context_processors.debug",
"django.template.context_processors.request",
"django.contrib.auth.context_processors.auth",
"django.contrib.messages.context_processors.messages",
],
},
},
]
WSGI_APPLICATION = "backend.wsgi.application"
DATABASES = {
"default": {
"ENGINE": "django.db.backends.sqlite3",
"NAME": BASE_DIR / "db.sqlite3",
}
}
AUTH_PASSWORD_VALIDATORS = [
{
"NAME": "django.contrib.auth.password_validation.UserAttributeSimilarityValidator",
},
{
"NAME": "django.contrib.auth.password_validation.MinimumLengthValidator",
},
{
"NAME": "django.contrib.auth.password_validation.CommonPasswordValidator",
},
{
"NAME": "django.contrib.auth.password_validation.NumericPasswordValidator",
}
]
LANGUAGE_CODE = "en-us"
TIME_ZONE = "UTC"
USE_I18N = True
USE_TZ = True
STATIC_URL = "static/"
DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"
# AI 叫我加的
CORS_ORIGIN_ALLOW_ALL = True
CORS_ALLOW_CREDENTIALS = True
感謝各位大大
https://docs.djangoproject.com/zh-hans/4.2/howto/deployment/
幾個回答:
為啥要用wsgi/asgi?
https://stackoverflow.com/questions/35657332/django-difference-between-using-server-through-manage-py-and-other-servers-like
反正就是效能以及管理考量。你真的不要用當然也可以。但正式生產環境強烈推薦你用。
用NGINX的話,不會吃到開發時的靜態檔案設定,要直接設定NGINX去處理靜態檔案。
https://docs.djangoproject.com/zh-hans/4.2/howto/static-files/#deployment
collectstatic會幫你把所有的靜態資源收集到前面設定的STATIC_ROOT裡面,你還需要在NGINX裡面設定,讓NGINX去處理靜態資源。
location / {
proxy_pass http://127.0.0.1:8000/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
// 處理靜態資源
location /static/ {
alias /your/path/dj3/static/;
}
我猜你index.html裡有寫死http://127.0.0.1:8000/chclass/log
的地方,你不能寫死,要改成正確的路徑
https://medium.com/tsungs-blog/day28-%E7%B6%B2%E9%A0%81%E7%9B%B8%E5%B0%8D-%E7%B5%95%E5%B0%8D%E8%B7%AF%E5%BE%91-%E8%AA%9E%E6%84%8F%E5%8C%96%E6%A8%99%E7%B1%A4-7c386e8032ab#b212/chclass/log
嗨 感謝您的回答 這裡還有幾個小問題想問您,但在此先回答您的回覆
http{
sever{
listen 443 ssl;
# .....
location / {
proxy_pass http://127.0.0.1:8000/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
# 處理靜態資源
location /static/ {
alias /python/backend/static/;
}
}
}
這樣子嗎
3. 的確我有寫死 但我直接用
let ajax=new XMLHttpRequest()
ajax.open("GET","/chclass/log/")
ajax.send()
他會真的導向至
http://localhost/chclass/log/
而非預期的http://localhost:8000/chclass/log/
這個部份是我哪裡有理解錯嗎
最後加問個問題 您知道favicon.ico的問題要如何解決嗎?
(存在c:/nginx/htdocs/favicon.ico)
再次感謝您的回答及協助,我在去研究一下您提供的文檔,如果突然理解了再告知您!
痾當我沒說他已經正確串接到後端了
也都正常了 謝謝您