[英]NGINX Serve Static Files from Sub Folder Within Ruby Rails
Running a ruby on rails application but have wordpress integrated under the /blog on the domain. 在Rails应用程序上运行ruby,但在域的/ blog下集成了wordpress。
The problem I'm having is that none of the asset files are served correctly under the /blog url. 我遇到的问题是/ blog网址下没有正确提供资产文件。
The wordpress php files are routed correctly and work. wordpress php文件可以正确路由并正常工作。 The issue is that I'm trying to route the wordpress theme and plugin files, namely css and js files to the /blog folder.
问题是我正在尝试将wordpress主题和插件文件(即css和js文件)路由到/ blog文件夹。 However I'm getting 404 for the static files served under /blog so I think I have a misconfiguration in my nginx conf file.
但是我在/ blog下得到的静态文件是404,所以我认为我的nginx conf文件配置错误。
Current nginx configuration: 当前的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 *;
}
}
There is a difference between root and alias , I think you're looking for alias
in this situation. root和alias之间有区别,我认为您正在这种情况下寻找
alias
。
When you use root
nginx appends the URI to the path, so using root /var/www/wordpress/current/blog;
当您使用
root
nginx将URI附加到路径,因此使用root /var/www/wordpress/current/blog;
will cause this to be the root directory for the request, which means navigating to /blog/css/style.css
will cause nginx to look for /var/www/wordpress/current/blog/blog/css/style.css
. 将导致该目录成为请求的根目录,这意味着导航到
/blog/css/style.css
将导致nginx查找/var/www/wordpress/current/blog/blog/css/style.css
。
If you use an alias instead, then nginx will map the uri to the directory: 如果您改用别名,则nginx会将uri映射到目录:
alias /var/www/wordpress/current/blog;
When you navigate to /blog/css/style.css
nginx will remove the prefix and serve the file from /var/www/wordpress/current/blog/css/style.css
, it seems you're attempting to do this with a rewrite however your rewrite is rewriting the request to the same uri. 当您导航到
/blog/css/style.css
nginx会删除前缀并从/var/www/wordpress/current/blog/css/style.css
提供文件,似乎您正在尝试使用重写,但是您的重写会将请求重写为相同的uri。
In the situation the URL doesn't work your error_log should be your friend, it'll tell you exactly where it's looking: 在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"
Changing this to alias throws an error for me (because I don't have your directory structure) but it shows how the location changes: 将其更改为别名会对我抛出错误(因为我没有您的目录结构),但是它显示了位置如何更改:
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"
You also don't have a lot of duplicate directives, you only need to define the them once as they are inherited by children, this can clean up your configuration file a lot making it easier to switch things around in the future: 您也没有很多重复的指令,只需要定义一次即可,因为它们被子代继承,这可以清理很多配置文件,从而使将来切换时更容易:
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;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.