
我在 Ubuntu 伺服器上安裝了 Nginx,並在blog.example.com
.
在 WordPress 上,我創建了一個畫廊頁面,可以從blog.example.com/gallery
.
現在,當有人造訪 時,gallery.example.com
它應該顯示 的內容blog.example.com/gallery
。我怎樣才能在 Nginx 中實現這一目標?
這是我在 nginx 伺服器設定上嘗試過的blog.example.com.config
:
location /gallery {
proxy_pass http://gallery.example.com/;
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;
}
答案1
正確配置
你的代理方式是錯誤的!它是從路徑到子域的代理;最有可能與路徑所在的主機名稱相同。如果是 with server_name gallery.example.com
,則目前代理 fromhttp://gallery.example.com/gallery
到http://gallery.example.com/
。
要使其代理 fromhttp://gallery.example.com
到http://blog.example.com/gallery
:
server {
server_name gallery.example.com;
location / {
proxy_pass http://blog.example.com/gallery;
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;
}
}
使用目前代理配置的拒絕服務攻擊
完整的錯誤配置:
server {
server_name gallery.example.com;
location /gallery {
proxy_pass http://gallery.example.com/;
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;
}
}
這種錯誤配置與代理位於同一子網域上的細節非常有趣,因為有人實際上可以透過存取 URL 來濫用這種設定來進行拒絕服務,這會導致與/gallery
URL一樣多的內部代理連接各部分相互跟隨。
http://gallery.example.com/gallery/gallery/gallery/gallery/gallery/.../gallery
=> . . .
=> http://gallery.example.com/gallery/gallery/gallery/gallery/gallery/gallery
=> http://gallery.example.com/gallery/gallery/gallery/gallery/gallery
=> http://gallery.example.com/gallery/gallery/gallery/gallery
=> http://gallery.example.com/gallery/gallery/gallery
=> http://gallery.example.com/gallery/gallery
=> http://gallery.example.com/gallery
=> http://gallery.example.com/
呼叫許多此類請求會導致伺服器上的高負載,這種負載在攻擊停止後可能會持續很長時間,因為 Nginx 仍在嘗試為所有這些巢狀代理連線提供服務。 Nginx 也可以開始向合法使用者顯示502 Bad Gateway
& 。500 Internal Server Error
即使記錄所有這些請求也可能成為一個問題:[alert] 1620#1620: *2748894 write() to "/var/log/nginx/access.log" was incomplete: 3421 of 4566 while logging request
如果這是一台https://
伺服器,所有這些 TLS 握手都會導致更高的負載。
單一 HTTP 請求可能會引起多少個代理連線?
這large_client_header_buffers
控制最大請求行長度,預設為4 8k
8096 個 ASCII 字元。雖然http2_max_field_size
已過時,快速測試curl
顯示預設情況下 Nginx 可以透過 HTTP/2 提供最多 11157 個字元長的 URL。無論是哪一個限制,您的代理程式都會/gallery
在發生錯誤之前允許 URL 中出現 1011 到 1394 次414 Request-URI Too Large
。
限制為worker_connections
如果不從預設值增加,可能會先受到打擊512
。這將導致500 Internal Server Error
錯誤日誌行包含:
512 worker_connections are not enough while connecting to upstream