我无法停止收到 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;
}
当我向服务器发出获取请求时,日志的输出如下:
" 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
配置如何处理请求,您是否有任何其他配置,例如在/etc/nginx/conf.d
或在/etc/nginx/sites-enabled
。
您可以使用server
块和location
块来配置代理,也可以使用root
指令将其设置为目录以从中发送文件。
因此为了帮助您更好地指定您想用 nginx 做什么。