2017-06-15 92 views
0

在rails应用程序上运行ruby,但在域的/ blog下集成了wordpress。NGINX从Ruby文件夹中的子文件夹中提取静态文件

我遇到的问题是没有任何资产文件在/ 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/[email protected]/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 *; 
    } 

} 

回答

0

rootalias之间的差异,我觉得你在这种情况下,寻找alias

当您使用nginx的root追加的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" 

此更改为别名抛出一个错误,我(因为我不“T有你的目录结构),但它显示了如何将位置变化:

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/[email protected]/wrappers/ruby; 
     passenger_enabled    on; 
     passenger_max_request_queue_size 200; 
     rails_env      production; 
    } 
} 
相关问题