nginx 配置和上游服务器出现问题

nginx 配置和上游服务器出现问题

我正在尝试配置两个由 nginx 提供服务的应用程序。第一个应用程序似乎运行良好,我复制了第二个应用程序的配置,但是遇到了一些问题。

我正在运行一个由 Unicorn 支持的 Sinatra 服务器,使用套接字传递上游信息。我可以顺利访问根域 (api.richardson.co.nz),一切似乎都运行正常,但是,当我尝试访问根域之外的路径 (api.richardson.co.nz/games) 时,我收到“未找到”错误。

这是我的 nginx conf 文件:

worker_processes 1;
user nginx web;
pid /tmp/nginx.pid;
error_log /var/www/knowyourgenre.com/shared/log/nginx.error.log;

events {
  worker_connections 1024; # increase if you have lots of clients
  accept_mutex off; # "on" if nginx worker_processes > 1
  use epoll; # enable for Linux 2.6+
}

http {
  include mime.types;
  default_type application/octet-stream;
  access_log /tmp/nginx.access.log combined;
  sendfile on;
  tcp_nopush on; # off may be better for *some* Comet/long-poll stuff
  tcp_nodelay off; # on may be better for some Comet/long-poll stuff
  gzip on;
  gzip_http_version 1.0;
  gzip_proxied any;
  gzip_min_length 500;
  gzip_disable "MSIE [1-6]\.";
  gzip_types text/plain text/html text/xml text/css
             text/comma-separated-values
             text/javascript application/x-javascript
             application/atom+xml;

  upstream app_server {
    server unix:/var/www/knowyourgenre.com/current/unicorn.sock fail_timeout=0;
    server localhost:8080 fail_timeout=0;
  }

  upstream api {
    server unix:/var/www/api/unicorn.sock fail_timeout=0;
    server localhost:8081 fail_timeout=0;
  }

  server {
    listen 80; # for Linux
    client_max_body_size 4G;
    server_name .knowyourgenre.com;
    keepalive_timeout 5;
    root /var/www/knowyourgenre.com/current/public/;
    try_files $uri/index.html $uri.html $uri @app;

    location @app {
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header Host $http_host;
      proxy_redirect off;
      proxy_pass http://app_server;
    }

    error_page 500 502 503 504 /500.html;
    location = /500.html {
      root /path/to/app/current/public;
    }
  }

  server {
    listen 80; # for Linux
    client_max_body_size 4G;
    server_name .richardson.co.nz;
    keepalive_timeout 5;
    root /var/www/api;
    try_files $uri/index.html $uri.html $uri @app;

    location @app {
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header Host $http_host;
      proxy_redirect off;
      proxy_pass http://api;
    }

    # Rails error pages
    error_page 500 502 503 504 /500.html;
    location = /500.html {
      root /path/to/app/current/public;
    }
  }

}

上游服务器中的错误是否会导致返回“未找到”?我很确定 Sinatra 服务器中一切正常,但我可能遗漏了一些东西。

答案1

在根值的末尾添加斜杠。

root /var/www/api/;

另外,我建议将“try_files”包装在某个位置。

location / {
    try_files $uri/index.html $uri.html $uri @app;
}

相关内容