多个上游和 nginx 子域名不起作用

多个上游和 nginx 子域名不起作用

我正在尝试在 nginx 中设置一个子域,该子域代理并调用在后面运行的 Web 服务器。我正在尝试这样做api.mywebsite.com。我有以下 nginx.conf 文件。

user       nginx nginx;  ## Default: nobody
worker_processes  5;  ## Default: 1
error_log  logs/error.log;
pid        logs/nginx.pid;
worker_rlimit_nofile 8192;

events {
  worker_connections  4096;  ## Default: 1024
}

 http {
    proxy_cache_path  /var/cache/nginx levels=1:2 keys_zone=one:8m max_size=3000m inactive=600m;
    proxy_temp_path /var/tmp;
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;

    gzip on;
    gzip_comp_level 6;
    gzip_vary on;
    gzip_min_length  1000;
    gzip_proxied any;
    gzip_types text/plain text/html text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
    gzip_buffers 16 8k;

    upstream first_upstream {
      server localhost:8080;
      server localhost:8081;
      server localhost:8082;
      server localhost:8083;
      server localhost:8084;
      keepalive 64;
    }

    upstream api_upstream {
      server localhost:9090;
      keepalive 64;
    }


    server {
      listen 80;
      server_name mywebsite.com;
      return 301 $scheme://www.mywebsite.com$request_uri;
    }

    server {
        listen 80;
        server_name www.mywebsite.com;
        error_page 502  /errors/502.html;

        location ~ ^/(images/|img/|javascript/|js/|css/|stylesheets/|flash/|media/|static/|robots.txt|humans.txt|favicon.ico) {
          root /home/mywebsite/website/public;
          access_log off;
          expires max;
        }

        location / {
          proxy_redirect off;
          proxy_set_header   X-Real-IP            $remote_addr;
          proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
          proxy_set_header   X-Forwarded-Proto $scheme;
          proxy_set_header   Host                   $http_host;
          proxy_set_header   X-NginX-Proxy    true;
          proxy_set_header   Connection "";
          proxy_http_version 1.1;
          proxy_cache one;
          proxy_cache_key sfs$request_uri$scheme;
          proxy_pass         http://first_upstream;
        }
    }

    server {
        listen 80;
        server_name api.mywebsite.com;
        location / {
          proxy_pass         http://api_upstream;
        }
    }
}

修改 conf 文件后,我重新加载并重新启动了 nginx。但新的子域名 (api.mywebsite.com) 无法访问。有人能帮我找出我做错的地方吗?

相关内容