2017-06-15 13 views
0

Ruby on Railsアプリケーションを実行していますが、ドメイン上の/ blogの下にワードプレスが統合されています。NGINX Ruby Rails内のサブフォルダから静的ファイルを配信

私が抱えている問題は、アセットファイルが/ブログのURLの下に正しく配信されていないことです。

wordpressのphpファイルは正しくルーティングされて動作します。問題は、WordPressのテーマやプラグインファイル、つまりcssとjsファイルを/ 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を探していると思います。

あなたはroot nginxのは、そう、これは/var/www/wordpress/current/blog/blog/css/style.cssを探すことnginxの原因となります/blog/css/style.cssにナビゲートすることを意味要求のルートディレクトリにさせるだろうroot /var/www/wordpress/current/blog;を使用して、パスにURIを追加使用し

あなたの代わりにエイリアスを使用する場合は、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" 

をエイリアスにこれを変更すると、私のためにエラーがスローされます(Iドン理由「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; 
    } 
} 
関連する問題