为什么nginx默认为我的proxypass?

时间:2017-03-31 17:02:27

标签: nginx

我使用nginx作为代理服务器,在单独的端口上运行的单个服务器上提供两个Web应用程序(用于本地开发)。下面是完整的nginx.conf文件:

user www-data;
worker_processes auto;
pid /run/nginx.pid;

events {
    worker_connections 768;
    # 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;

    ##
    # SSL Settings
    ##

    ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
    ssl_prefer_server_ciphers on;

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

    ##
    # Virtual Host Configs
    ##

    server {
      server_name news.mysite;
      location / {
        proxy_pass http://localhost:3001;
      }
    }

    server {
      server_name blog.mysite;
      location / {
        proxy_pass http://localhost:3002;
      }
    }

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

从浏览器访问子域时,我会根据正在运行的Web应用看到预期的内容。但是,当我通过浏览器(http://mysite)访问主域时,它会显示第一个proxypass(news.mysite @ localhost:3001)中的内容。我原本期望以下两种情况之一:

  • 从默认目录@ / var / www / html
  • 提供内容
  • 浏览器中典型的“无法访问此站点”错误。

为什么nginx默认代理它找到的第一个代理通道,如何更改呢?

1 个答案:

答案 0 :(得分:1)

nginx遇到套接字的第一个server将被视为默认值,除非您创建另一个明确标记为默认值。{/ p>

因此,对于您的情况,您可能希望添加一个额外的server块作为全部捕获:

server {
   listen 80 default_server;
   root /var/www/html;
}