Ruby on rails NGINX提供Ruby-Rails子文件夹中的静态文件

Ruby on rails NGINX提供Ruby-Rails子文件夹中的静态文件,ruby-on-rails,wordpress,nginx,Ruby On Rails,Wordpress,Nginx,运行RubyonRails应用程序,但将wordpress集成在域的/blog下 我遇到的问题是,没有一个资产文件在/blog url下得到正确的服务 wordpress php文件已正确路由并正常工作。问题是,我正在尝试将wordpress主题和插件文件,即css和js文件路由到/blog文件夹。然而,我在/blog下得到的静态文件是404,所以我认为我的nginx conf文件中有一个配置错误 当前nginx配置: server { listen 3000; server

运行RubyonRails应用程序,但将wordpress集成在域的/blog下

我遇到的问题是,没有一个资产文件在/blog url下得到正确的服务

wordpress php文件已正确路由并正常工作。问题是,我正在尝试将wordpress主题和插件文件,即css和js文件路由到/blog文件夹。然而,我在/blog下得到的静态文件是404,所以我认为我的nginx conf文件中有一个配置错误

当前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 *;
   }

 }

和之间存在差异,我认为在这种情况下,您需要的是
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

在URL不起作用的情况下,您的错误日志应该是您的朋友,它会准确地告诉您它正在查找的位置:

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"
将此更改为alias会为我抛出一个错误(因为我没有您的目录结构),但它显示了位置如何更改:

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