Ubuntu

讓 SSL 在 Django 項目上工作

  • February 24, 2016

我正試圖讓我的 Django 網站完全移動到https(目前它只是http)。網路伺服器是 nginx(反向代理)和 gunicorn。

但是,即使在正確安裝 SSL、打開埠 443 並調整我的 nginx 虛擬主機文件之後,我也無法連接到我的網站https://example.comhttp://example.com工作正常)。有人可以指導我如何解決這個問題嗎?

以下是詳細資訊:

/etc/iptables/rules.v4(我使用iptables-persistent包)中,我在其他行中有這個片段:

# Acceptable TCP traffic
-A TCP -p tcp --dport 22 -j ACCEPT
-A TCP -p tcp --dport 80 -j ACCEPT
-A TCP -p tcp --dport 443 -j ACCEPT

如果我寫sudo netstat -4plunt,輸出顯示埠 443 正在監聽:

Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      18445/nginx     
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      923/sshd        
tcp        0      0 127.0.0.1:5432          0.0.0.0:*               LISTEN      719/postgres    
tcp        0      0 0.0.0.0:443             0.0.0.0:*               LISTEN      18445/nginx    

文件夾中的我的 nginx 虛擬主機文件sites-enabled包含以下程式碼:

server {
   listen 80;

   listen 443 ssl;
   server_name example.com www.example.com;
   ssl_certificate /etc/ssl/certs/ssl-bundle.crt;
   ssl_certificate_key /etc/ssl/private/myserver.key;
   ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
   ssl_prefer_server_ciphers on;
   ssl_ciphers 'EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH';

   charset utf-8;
   underscores_in_headers on;

   location = /favicon.ico { access_log off; log_not_found off; }

   location /static/ {

       root /home/myuser/myprojectfolder/myproject;
   }

   location /static/admin/ {

       root /home/myuser/.virtualenvs/myenv/local/lib/python2.7/site-packages/django/contrib/admin/static/;
   }

   location /status {
       stub_status on;
       allow 127.0.0.1;
       allow 40.114.247.165;
       deny all;
   }

   location / {
       proxy_pass_request_headers on;
       proxy_buffering on;
       proxy_buffers 8 24k;
       proxy_buffer_size 2k;
       include proxy_params;
       #include /etc/nginx/naxsi.rules;
       #include /etc/nginx/naxsi_whitelist.rules;
       proxy_pass          http://unix:/home/myuser/myprojectfolder/myproject/myproject.sock;

   }


   error_page 500 502 503 504 /500.html;
   location = /500.html {
       root /home/myuser/myprojectfolder/myproject/templates/;
  }
}

最後,我將以下行添加到我的 django 項目settings.py文件中:

SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')

完成上述所有更改後,我仍然無法執行我的網站https;連接總是超時。

有人可以幫助我從哪裡開始解決這個問題?如果您需要,請向我詢問更多資訊。


完整的規則集/etc/iptables/rules.v4如下:

*filter
# Allow all outgoing, but drop incoming and forwarding packets by default
:INPUT DROP [0:0]
:FORWARD DROP [0:0]
:OUTPUT ACCEPT [0:0]

# Custom per-protocol chains
:UDP - [0:0]
:TCP - [0:0]
:ICMP - [0:0]

# Acceptable UDP traffic

# Acceptable TCP traffic
-A TCP -p tcp --dport 22 -j ACCEPT
-A TCP -p tcp --dport 80 -j ACCEPT
-A TCP -p tcp --dport 443 -j ACCEPT

# Acceptable ICMP traffic

# Boilerplate acceptance policy
-A INPUT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
-A INPUT -i lo -j ACCEPT

# Drop invalid packets
-A INPUT -m conntrack --ctstate INVALID -j DROP

# Pass traffic to protocol-specific chains
## Only allow new connections (established and related should already be handled)
## For TCP, additionally only allow new SYN packets since that is the only valid
## method for establishing a new TCP connection
-A INPUT -p udp -m conntrack --ctstate NEW -j UDP
-A INPUT -p tcp --syn -m conntrack --ctstate NEW -j TCP
-A INPUT -p icmp -m conntrack --ctstate NEW -j ICMP

# Reject anything that's fallen through to this point
## Try to be protocol-specific w/ rejection message
-A INPUT -p udp -j REJECT --reject-with icmp-port-unreachable
-A INPUT -p tcp -j REJECT --reject-with tcp-reset
-A INPUT -j REJECT --reject-with icmp-proto-unreachable

# Commit the changes
COMMIT

*raw
:PREROUTING ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
COMMIT

*nat
:PREROUTING ACCEPT [0:0]
:INPUT ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
:POSTROUTING ACCEPT [0:0]
COMMIT

*security
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
COMMIT

*mangle
:PREROUTING ACCEPT [0:0]
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
:POSTROUTING ACCEPT [0:0]
COMMIT

/proc/net/ip_tables_matches包含:

conntrack
conntrack
conntrack
udplite
udp
tcp
icmp

proxy_params包含在我的 nginx 虛擬主機文件中包含以下內容:

proxy_set_header Host $host;
proxy_set_header User-Agent $http_user_agent;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Remote-Addr $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;

連接可能會超時,因為您的 Iptables 在某些時候可能不正確。

嘗試刷新您的 Iptables(如果您有一些工作,請先保存它們)在刷新測試之後添加這些會很好。

-A INPUT -p tcp --dport 80 -m state --state NEW -j ACCEPT
-A INPUT -p tcp --dport 443 -m state --state NEW -j ACCEPT

# Allow inbound traffic from established connections.
# This includes ICMP error returns.
-A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

引用自:https://serverfault.com/questions/758709