NGINX从Ruby Rails中的子文件夹提供静态文件

时间:2017-06-15 00:29:41

标签: ruby-on-rails wordpress nginx

在rails应用程序上运行ruby,但在域上的/ blog下集成了wordpress。

我遇到的问题是没有任何资产文件在/ blog网址下正确提供。

wordpress php文件正确路由并正常工作。问题是我正在尝试将wordpress主题和插件文件(即css和js文件)路由到/ blog文件夹。但是我在/ blog下提供的静态文件获得404,所以我认为我的nginx配置文件中的配置错误。

当前的nginx配置:

server {
  listen       3000;
  server_name  myapp.com;
  access_log off;

    location /blog {
      location ~* ^.+\.(jpg|jpeg|gif|png|css|bmp|js|ico|swf)$ {
        expires max;
        access_log off;
        add_header Cache-Control public;
        root /var/www/wordpress/current/blog;
        break;
      }

      root /var/www/wordpress/current/blog;
      index index.php index.html index.htm;
      rewrite ^/blog/(.*)$ /blog/$1 break;
      try_files $uri $uri/ /index.php?$args;
     }

    location ~* ^.+\.(jpg|jpeg|gif|png|css|bmp|js|ico|swf)$ {
      root  /u/apps/myapp/current/public;
      expires max;
    }

    if (-f $request_filename.html) {
     rewrite (.*) $1.html break;
    }

    location ~*  \.(jpg|jpeg|png|gif|ico|css|js)$ {
      expires max;
      access_log off;
      add_header Cache-Control public;
      root /u/apps/myapp/current/public;
      break;
    }

    client_max_body_size 50M;
    root /u/apps/myapp/current/public;
     access_log off;
passenger_ruby /home/deploy/.rvm/gems/ruby-2.3.3@myapp/wrappers/ruby;
    passenger_enabled on;
passenger_max_request_queue_size 200;
    rails_env production;


    if ($host != 'myapp.com') {
      rewrite  ^/(.*)$  http://myapp.com/$1  permanent;
    }

    location ~* ^/assets/ {
      expires 1y;
      add_header Cache-Control public;

      add_header Last-Modified "";
      add_header ETag "";
      break;
    }


    error_page   500 504  /500.html;
    location = /500.html {
      root   /u/apps/myapp/current/public;
    }

    error_page   502 503  /503.html;
    location = /503.html {
      root   /u/apps/myapp/current/public;
    }

    error_page  404              /404.html;

    location = /50x.html {
        root   html;
    }

     location ~ .*\.php$ {
    root /var/www/wordpress/current;
        #fastcgi_split_path_info ^((?U).+\.php)(/?.+)$;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
        fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
    fastcgi_param  HTTPS 'on';
        include fastcgi_params;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index index.php;
    }

   location ~* "^.*?\.(eot)|(ttf)|(woff)$" {
     add_header Access-Control-Allow-Origin *;
   }

 }

1 个答案:

答案 0 :(得分:0)

rootalias之间存在差异,我认为您在这种情况下正在寻找alias

使用root时,nginx会将URI附加到路径,因此使用root /var/www/wordpress/current/blog;会导致该请求成为根目录,这意味着导航到/blog/css/style.css会导致nginx寻找/var/www/wordpress/current/blog/blog/css/style.css

如果您使用别名,则nginx会将uri映射到目录:

alias /var/www/wordpress/current/blog;

当您导航到/blog/css/style.css时,nginx将删除前缀并从/var/www/wordpress/current/blog/css/style.css提供文件,但您似乎正在尝试重写,但是您的重写会将请求重写为同样的uri。

如果网址不起作用,您的error_log应该是您的朋友,它会告诉您它的确切位置:

2017/06/15 13:04:19 [error] 43391#0: *1786 open()
  "/var/www/wordpress/current/blog/blog/css/styles.css" failed 
  (2: No such file or directory), client: 127.0.0.1, server: myapp.com, 
  request: "GET /blog/css/styles.css HTTP/1.1", host: "myapp.com:3000"

将此更改为别名会为我抛出错误(因为我没有您的目录结构),但它显示了位置的变化:

2017/06/15 13:06:12 [error] 43582#0: *1787 open() 
  "/var/www/wordpress/current/blog/css/styles.css" failed
  (2: No such file or directory), client: 127.0.0.1, server: myapp.com,
  request: "GET /blog/css/styles.css HTTP/1.1", host: "myapp.com:3000"

你也没有很多重复的指令,你只需要定义它们一次,因为它们是由孩子继承的,这可以清理你的配置文件很多,这样可以更容易地在未来:

server {
    client_max_body_size 50M;
    listen               3000;
    server_name          myapp.com;
    access_log           off;
    root                 /u/apps/myapp/current/public; # default root, use this unless specified otherwise
    error_page           500 504  /500.html;
    error_page           502 503  /503.html;
    error_page           404      /404.html;

    location /blog {
        alias     /var/www/wordpress/current/blog; # overwrite the default root for this entire block
        index     index.php index.html index.htm;
        try_files $uri $uri/ /index.php?$args;

        location ~* ^.+\.(jpg|jpeg|gif|png|css|bmp|js|ico|swf)$ {
            expires    max;
            add_header Cache-Control public;
            break;
        }
    }

    location ~* ^.+\.(jpg|jpeg|gif|png|css|bmp|js|ico|swf)$ {
        expires max;
    }

    location ~*  \.(jpg|jpeg|png|gif|ico|css|js)$ {
        expires max;
        add_header Cache-Control public;
        break;
    }

    location ~* "^.*?\.(eot)|(ttf)|(woff)$" {
        add_header Access-Control-Allow-Origin *;
    }

    if (-f $request_filename.html) {
        rewrite (.*) $1.html break;
    }

    if ($host != 'myapp.com') {
        rewrite  ^/(.*)$  http://myapp.com/$1  permanent;
    }

    location ~* ^/assets/ {
        expires    1y;
        add_header Cache-Control public;
        add_header Last-Modified "";
        add_header ETag "";
        break;
    }

    location = /50x.html {
        root html; # overwrite the default root for this
    }

    location ~ .*\.php$ {
        root          /var/www/wordpress/current; # overwrite the default root, because this doesn't have /blog on the end it will properly map to /var/www/wordpress/current/blog when /blog is accessed

        #fastcgi_split_path_info ^((?U).+\.php)(/?.+)$;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
        fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
        fastcgi_param HTTPS 'on';
        include       fastcgi_params;
        fastcgi_pass  127.0.0.1:9000;
        fastcgi_index index.php;
    }

    # this block is only processed if nothing else matches
    location / {
        passenger_ruby                   /home/deploy/.rvm/gems/ruby-2.3.3@myapp/wrappers/ruby;
        passenger_enabled                on;
        passenger_max_request_queue_size 200;
        rails_env                        production;
    }
}