
我想使用 nginx 配置我的 dockerized django 應用程式並在 EC2 執行個體上運行它,但我無法讓它工作。我是 nginx 的初學者。
我這樣做時的錯誤docker-compose -f production.yml up
是:
客戶端在讀取客戶端請求行時發送了無效方法,客戶端:80.153.184.19,伺服器:ec2-XXXXX.eu-central-2.compute.amazonaws.com,請求:「�U�F'�Io �q�+! �P�
我就像什麼?沒有任何基本配置可以使其順利運作嗎?
我的 nginx 設定 myconf:
server {
set $my_host $host;
if ($host ~ "\d+\.\d+\.\d+\.\d+") {
set $my_host "ec2-XXXXX.eu-central-2.compute.amazonaws.com";
}
listen 80;
server_name ec2-XXXXX.eu-central-2.compute.amazonaws.com;
charset utf-8;
error_log /dev/stdout info;
access_log /dev/stdout;
location / {
proxy_pass http://django:5000;
proxy_set_header Host $my_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
我的碼頭工人組成:
version: '3'
#volumes:
# production_caddy: {}
services:
django:
build:
context: .
dockerfile: ./compose/production/django/Dockerfile
image: heatbeat_website_production_django
env_file:
- ./.envs/.production/.django
- ./.envs/.production/.postgres
command: /start
nginx:
build:
context: .
dockerfile: ./compose/production/nginx/Dockerfile
ports:
- "0.0.0.0:8080:80"
depends_on:
- django
redis:
image: redis:5.0
我的 nginx dockerfile:
FROM tutum/nginx
RUN rm /etc/nginx/sites-enabled/default
COPY ./compose/production/nginx/myconf.conf /etc/nginx/sites-enabled/default
我非常感謝任何幫助...我很困惑...提前致謝!
答案1
我對這個部分很好奇:
set $my_host $host;
if ($host ~ "\d+\.\d+\.\d+\.\d+") {
set $my_host "ec2-XXXXX.eu-central-2.compute.amazonaws.com";
}
我在我的應用程式日誌記錄中看到這樣的異常:
django.core.exceptions.DisallowedHost: Invalid HTTP_HOST header: '54.148.178.171'...
當我進行反向代理時,我看到IP位於AWS內部。但我無法辨識資源。我發現您正在使用規範的伺服器名稱並將 IP 重寫給它,但這是否違背了 ALLOWED_HOSTS 的目的?
好吧,我明白為什麼你的解決方案讓我不舒服了。您正在獲取任何 IP 位址並映射它。典型的問題是捕獲隨時間變化的 ELB IP 位址。因此,將其放在位置區塊中要安全得多。在下面的範例中,我的 ALLOWED_HOSTS 清單中有“aws.elb.com”,並且在 ELP Health Check 標籤上,我將 ping 目標指定為 ELB-health-check。這應該是一種更安全的方法。
location /ELB-health-check {
set $my_host $host;
if ($host ~ "\d+\.\d+\.\d+\.\d+") {
set $my_host "aws.elb.com";
}
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
# set $my_host "aws.elb.com";
proxy_set_header Host $my_host;
# now send the request to the app
proxy_pass http://app_server;
}