开发者

How can I get nginx to serve static files from two locations when also serving a unicorn rails server?

Ok, so I have pretty much the standard nginx config for serving a unicorn rails server (listens to a socket file and also serves static files from the rails_app/public directory).

However, I want to do the following:

  1. serve static files from rails_app/public (as currently is done)
  2. serve static files with url /reports/ from a different root (like /mnt/files/)

I tried adding the following to my nginx config:

location /reports/ {
    root /mnt/matthew/web;
}

but it didn't work.

Any ideas how I can get this to happen?

(below is my entire nginx.conf file:

worker_processes 1;

pid /tmp/nginx.pid;
error_log /tmp/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+
  # use kqueue; # enable for FreeBSD, OSX
}

http {
  # nginx will find this file in the config directory set at nginx build time
  include mime.types;

  # fallback in case we can't determine a type
  default_type application/octet-stream;

  # click tracking!
  access_log /tmp/nginx.a开发者_运维技巧ccess.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;

  # this can be any application server, not just Unicorn/Rainbows!
  upstream app_server {
    server unix:/home/matthew/server/tmp/unicorn.sock fail_timeout=0;


  }

  server {
    # enable one of the following if you're on Linux or FreeBSD
    listen 80 default deferred; # for Linux
    # listen 80 default accept_filter=httpready; # for FreeBSD


    client_max_body_size 4G;
    server_name _;

    keepalive_timeout 5;

    location /reports/ {
        root /mnt/matthew/web;
    }
    # path for static files
    root /home/matthew/server/public;



    try_files $uri/index.html $uri.txt $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;
    }

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


location @app is looking for the files in /home/matthew/server/public, as that's the parent root specified. If your try files statement is matching files in location /reports/ that has a different root, those files won't be found. You need to set things up like this:

location /reports/ {
    root /mnt/matthew/web;
    try_files $uri/index.html $uri.txt $uri.html $uri @foo;
}
root /home/matthew/server/public;
try_files $uri/index.html $uri.txt $uri.html $uri @app;

location @foo {
  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;

  root /mnt/matthew/web
}

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;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜