首页 > 解决方案 > 为HTTP流量配置Nginx的反向代理出现403

问题描述

我正在尝试在客户端连接到我的服务器( http://www.51ti.vip)的 centos 6.9_64 环境中使用 Nginx 作为反向代理。

Nginx 会将所有请求转发到其他后端服务器。通信在端口 80 上工作。

但是一旦设置了proxy_set_header XXXXX,访问的时候就会出现403。/var/log/nginx/error.log 中没有相关的错误信息。

问题出在哪里?

第 403 页 禁止

您无权访问此服务器上的 URL。

笔记:

配置:

/etc/nginx/nginx.conf 如下:

user root;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /var/run/nginx.pid;

 include /usr/share/nginx/modules/*.conf;

 events {
   worker_connections  1024;
 }


 http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                  '$status $body_bytes_sent "$http_referer" '
                  '"$http_user_agent" "$http_x_forwarded_for"';

     access_log  /var/log/nginx/access.log  main;

     sendfile            on;
     tcp_nopush          on;
     tcp_nodelay         on;
     keepalive_timeout   65;
     types_hash_max_size 2048;


     include             /etc/nginx/mime.types;
     default_type        application/octet-stream;
     include /etc/nginx/conf.d/*.conf;
   }

/etc/nginx/conf.d/default.conf 如下:

server {
    listen       80 default_server;
    server_name  47.75.249.199 "";
    root         /usr/share/nginx/html;

   # Load configuration files for the default server block.
   include /etc/nginx/default.d/*.conf;


location / {
    proxy_pass http://sq.otherserver.com;

    #Proxy Settings
    proxy_redirect off;

    #proxy_set_header Host $host;
    #proxy_set_header X-Real-IP $remote_addr;
    #proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  }

 error_page 404 /404.html;
    location = /40x.html {
 }

 error_page 500 502 503 504 /50x.html;
    location = /50x.html {
 }
}

标签: nginx-locationnginx-reverse-proxynginx-config

解决方案


最初的问题是由“proxy_set_header Host $host”引起的,proxy_set_header X-Real-IP 和 proxy_set_header X-Forwarded-For 没有问题。

但是还是不明白为什么?

Nginx:何时使用 proxy_set_header 主机 $host 与 $proxy_host

模块 ngx_http_proxy_module


推荐阅读