【问题标题】:NGINX Serve Static Files from Sub Folder Within Ruby RailsNGINX 从 Ruby Rails 中的子文件夹提供静态文件
【发布时间】:2017-06-15 11:15:17
【问题描述】:

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

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

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

 }

【问题讨论】:

    标签: ruby-on-rails wordpress nginx


    【解决方案1】:

    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。

    在 URL 不起作用的情况下,您的 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;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2018-09-01
      • 2013-07-12
      • 2018-05-17
      • 1970-01-01
      • 2023-03-18
      • 2020-06-10
      • 2019-07-01
      • 1970-01-01
      • 2018-10-23
      相关资源
      最近更新 更多