我無法停止收到 400 錯誤請求。經過一番研究,我發現這意味著由於配置錯誤而導致請求的標頭錯誤
這是我的nginx.conf
文件:
user nginx;
worker_processes auto;
events {
worker_connections 1000;
}
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
server_tokens off;
set_real_ip_from 192.168.0.0/16;
set_real_ip_from 172.16.0.0/12;
set_real_ip_from 10.0.0.0/8;
real_ip_header X-Forwarded-For;
real_ip_recursive on;
log_format main '$remote_addr - $remote_user "$request" '
'$status $body_bytes_sent $http_host "$http_referer" '
'"$http_user_agent"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
gzip on;
gzip_disable "msie6";
gzip_vary on;
gzip_proxied any;
gzip_comp_level 6;
gzip_buffers 16 8k;
gzip_http_version 1.1;
gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*.conf;
}
這是當我向伺服器發出 get 請求時日誌的輸出:
" while reading PROXY protocol, client: 172.26.0.5, server: 0.0.0.0:80
2021/09/16 11:08:07 [error] 8#8: *8 broken header: "GET /favicon.ico HTTP/1.1
pragma: no-cache
cache-control: no-cache
sec-ch-ua: "Google Chrome";v="93", " Not;A Brand";v="99", "Chromium";v="93"
sec-ch-ua-mobile: ?0
user-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.82 Safari/537.36
sec-ch-ua-platform: "Windows"
accept: image/avif,image/webp,image/apng,image/svg+xml,image/*,*/*;q=0.8
sec-fetch-site: same-origin
sec-fetch-mode: no-cors
sec-fetch-dest: image
referer: https://localhost/
accept-encoding: gzip, deflate, br
accept-language: en-US,en;q=0.9
host: localhost
X-Forwarded-For: 172.26.0.1
這個 nginx 反向代理在 Docker 容器上運行,具有以下內容dockerfile
:
FROM nginx:1.16
ENV DOCKER_IMAGE nginx
ENV DOCKER_NAME nginx
ENV TZ Africa/Tunis
RUN set -x && \
# tiny helper to reload nginx config
printf '#!/bin/bash\n/usr/sbin/nginx -s reload\n' >> /usr/local/bin/nginx-reload && \
chmod +x /usr/local/bin/nginx-reload && \
# delete nginx default server config
rm /etc/nginx/conf.d/default.conf
COPY ./conf/ /etc/
ENV WORKER_PROCESSES auto
ENV WORKER_CONNECTIONS 1024
ENV SERVER_NAME localhost
答案1
沒有nginx.conf
配置如何處理請求,您是否有任何其他配置,例如 in/etc/nginx/conf.d
或 in /etc/nginx/sites-enabled
。
您可以將server
區塊與location
區塊一起使用來配置代理,也可以使用root
指令將其設定為目錄以從中傳送檔案。
所以為了幫助你更好地指定你想用 nginx 做什麼。