nginx开启反向代理吗?

nginx开启反向代理吗?

不知何故,我不确定如何,我们配置了一个充当开放代理的 nginx 反向代理。

日志文件告诉我们匹配的请求针对的是以下conf文件:

map $http_upgrade $connection_upgrade {
        default upgrade;
        ''      close;
}

access_log      /var/log/nginx/access.p.log;
error_log       /var/log/nginx/error.p.log;

proxy_next_upstream     error timeout invalid_header http_500 http_502 http_503 http_504;
proxy_buffers           8 32k;
proxy_buffer_size       64k;
proxy_http_version      1.1;
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_redirect          off;
proxy_set_header        Upgrade         $http_upgrade;
proxy_set_header        Connection      $connection_upgrade;

server {
        listen          80;
        server_name     ~first.*\.second\.com;

        location / {
                resolver                172.16.1.1 172.16.1.2;
                proxy_pass              http://$host;
                proxy_read_timeout      7200s;
        }
}

server {
        listen          443;
        server_name     ~first.*\.second\.com;
        ssl             on;

        location / {
                resolver                172.16.1.1 172.16.1.2;
                proxy_pass              https://$host;
                proxy_read_timeout      7200s;
        }
}

顶层conf文件如下所示:

用户 nginx;worker_processes 8;pid /var/run/nginx.pid;

事件 { worker_connections 4096; # multi_accept on; }

http {

    ##
    # Basic Settings
    ##

    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;
    # server_tokens off;

    # server_names_hash_bucket_size 64;
    # server_name_in_redirect off;

    include /etc/nginx/mime.types;
    default_type application/octet-stream;

    ##
    # Logging Settings
    ##

    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;

    ##
    # Gzip Settings
    ##

    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/x-javascript text/xml application/xml application/xml+rss text/javascript;

    ##
    # nginx-naxsi config
    ##
    # Uncomment it if you installed nginx-naxsi
    ##

    #include /etc/nginx/naxsi_core.rules;

    ##
    # nginx-passenger config
    ##
    # Uncomment it if you installed nginx-passenger
    ##

    #passenger_root /usr;
    #passenger_ruby /usr/bin/ruby;

    ##

    include /etc/nginx/conf.d/*.conf;
    #include /etc/nginx/sites-enabled/*;

}

是什么原因造成的?

答案1

有文件nginx 如何处理请求。具体来说,这意味着如果您只有一个服务器块,那么无论Host标头如何,所有请求都将最终到达那里。

在您的例子中,您希望仅处理与您的主机匹配的请求并忽略其他请求。因此,您必须声明另一个服务器块来处理所有不匹配的请求。

它看起来应该是这样的:

server {
    listen 80 default_server;
    listen 443 default_server;
    return 403;
    # or return 444; to just close connection without response.
}

server {
    listen 80;
    server_name ~first.*\.second\.com;
    ....
}

server {
    listen 443 ssl;
    server_name ~first.*\.second\.com;
    ....
}

相关内容