在 nginx 上部署 rails 应用出问题了。总是显示:Welcome to nginx! 配置文件里:
http {
passenger_root /usr/local/rvm/gems/ruby-1.9.3-p125/gems/passenger-3.0.12;
passenger_ruby /usr/local/rvm/wrappers/ruby-1.9.3-p125/ruby;
-----------------------------------------------------------------------------------------------------------------------
server {
listen 80;
server_name localhost;
root /home/hlcfan/workspace/chatpro;
location / {
root html;
index index.html index.htm;
}
谢谢!
先把 nginx 默认的 index.html 文件删除,和 rails 要删除 public 目录下的 index.html 原理一样
然后把 server 中的 root 指向 rails app 的 public 目录
#2 楼 @willmouse 你好,我删了之后,403.
你说 root 指向 public 是这样:
root /home/hlcfan/workspace/chatpro/public;
我最后这么写,在我本机可以正常跑。但是到了服务器就有问题了.403.求解。
server {
listen 80;
server_name 106.187.94.74;
root /home/hlcfan/workspace/chatpro/public;
passenger_enabled on;
rails_env production;
#charset koi8-r;
#access_log logs/host.access.log main;
#location / {
# root /home/hlcfan/workspace/chatpro/public;
# index index.html index.htm;
#}
error.log 里依然访问/public/index.html....
2012/04/25 02:14:23 [error] 2819#0: *19 "/home/hlcfan/chatpro/public/index.html" is forbidden (13: Permission denied), client: 118.202.225.7, server: localhost, request: "GET / HTTP/1.1", host: "106.187.94.74"
修改后重启 nginx 了么? server_name localhost; => 要改成你自己的域名
而且你的 rvm 怎么安装到这里了?用 sudo 装的? passenger_root /usr/local/rvm/gems/ruby-1.9.3-p125/gems/passenger-3.0.12; passenger_ruby /usr/local/rvm/wrappers/ruby-1.9.3-p125/ruby;
`"/home/hlcfan/chatpro/public/index.html" is forbidden (13: Permission denied)
注意这段!
ls -lh
[hlcfan@li414-74 chatpro]$ ls -lh
total 1.8M
drwxrwxr-x 8 hlcfan hlcfan 4.0K Apr 21 06:04 app
drwxrwxr-x 5 hlcfan hlcfan 4.0K Apr 21 06:04 config
-rw-rw-r-- 1 hlcfan hlcfan 157 Apr 21 06:04 config.ru
drwxrwxr-x 2 hlcfan hlcfan 4.0K Apr 21 06:04 db
drwxrwxr-x 2 hlcfan hlcfan 4.0K Apr 21 06:04 doc
-rw-rw-r-- 1 hlcfan hlcfan 10 Apr 25 03:57 dump.rdb
-rw-rw-r-- 1 hlcfan hlcfan 1.7K Apr 21 08:16 Gemfile
-rw-rw-r-- 1 hlcfan hlcfan 4.6K Apr 23 09:11 Gemfile.lock
drwxrwxr-x 4 hlcfan hlcfan 4.0K Apr 21 06:04 lib
drwxrwxr-x 2 hlcfan hlcfan 4.0K Apr 21 07:11 log
-rw------- 1 hlcfan hlcfan 1.7M Apr 25 04:02 nohup.out
drwxrwxr-x 3 hlcfan hlcfan 4.0K Apr 25 03:26 public
-rw-rw-r-- 1 hlcfan hlcfan 272 Apr 21 06:04 Rakefile
-rw-rw-r-- 1 hlcfan hlcfan 41 Apr 21 06:04 README.rdoc
drwxrwxr-x 2 hlcfan hlcfan 4.0K Apr 21 06:04 script
-rwxrwxr-x 1 hlcfan hlcfan 134 Apr 22 07:00 start.sh
drwxrwxr-x 7 hlcfan hlcfan 4.0K Apr 21 06:04 test
drwxrwxr-x 6 hlcfan hlcfan 4.0K Apr 21 07:00 tmp
drwxrwxr-x 5 hlcfan hlcfan 4.0K Apr 21 07:07 vendor
Nginx Config
#user nobody;
worker_processes 1;
error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
use epoll;
}
http {
passenger_root /usr/local/rvm/gems/ruby-1.9.3-p194/gems/passenger-3.0.12;
passenger_ruby /usr/local/rvm/wrappers/ruby-1.9.3-p194/ruby;
#passenger_default_group root;
#passenger_default_user root;
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
#sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
gzip on;
server {
listen 80;
server_name 106.187.94.74;
root /home/hlcfan/chatpro/public;
passenger_enabled on;
rails_env production;
#charset koi8-r;
#access_log logs/host.access.log main;
#location / {
# root html;
# index index.html index.htm;
#}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
#some commented config
}
}
change #user nobody;
to user root
按理 ip 也是应该可以的,我看错你的问题了,我以为你还是"welcome to nginx"的问题
http://wiki.nginx.org/CoreModule#user
If the master process is run as root, then nginx will setuid()/setgid() to USER/GROUP. If GROUP is not specified, then nginx uses the same name as USER. By default it's nobody user and nobody or nogroup group or the --user=USER and --group=GROUP from the ./configure script.
nobody 属于其他用户,没有 w 的权限,为什么需要 w 的权限呢,留给你研究一下
#3 楼 @hlcfan 我也遇到同样的问题。但是我照你的修改。删除 index.html 后,报 403.然后 root 也改为 public 地址,并且重新加载了。但是还是报 403. 是什么原因呢?我感觉是 root 地址没有链接对
Nginx Config
#user nobody; worker_processes 1;
#error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info;
#pid logs/nginx.pid;
events { worker_connections 1024; }
http { passenger_root /Library/Ruby/Gems/1.8/gems/passenger-3.0.12; passenger_ruby /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin/ruby;
include mime.types; default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" ' # '$status $body_bytes_sent "$http_referer" ' # '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on; #tcp_nopush on;
#keepalive_timeout 0; keepalive_timeout 65;
#gzip on;
server { listen 80; server_name awesome_project.local; root /Users/wdlgame/Documents/RubyOnRailsWork/depot/public; passenger_enabled on;
#charset koi8-r;
#access_log logs/host.access.log main;
location / { root html; index index.html index.htm; }
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root html; }
# proxy the PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ .php$ { # proxy_pass http://127.0.0.1; #}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # #location ~ .php$ { # root html; # fastcgi_pass 127.0.0.1:9000; # fastcgi_index index.php; # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; # include fastcgi_params; #}
# deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /.ht { # deny all; #} }
# another virtual host using mix of IP-, name-, and port-based configuration # #server { # listen 8000; # listen somename:8080; # server_name somename alias another.alias;
# location / { # root html; # index index.html index.htm; # } #}
# HTTPS server # #server { # listen 443; # server_name localhost;
# ssl on; # ssl_certificate cert.pem; # ssl_certificate_key cert.key;
# ssl_session_timeout 5m;
# ssl_protocols SSLv2 SSLv3 TLSv1; # ssl_ciphers HIGH:!aNULL:!MD5; # ssl_prefer_server_ciphers on;
# location / { # root html; # index index.html index.htm; # } #} }
你的这个 root 没有改写成 rails 的 public 目录
location / { root html; index index.html index.htm; }
user root;
---- 后边需要加分号;
#location / {
# root html;
# index index.html index.htm;
#}
文件路径 public/upload/a.xls nginx 下 File.open('upload/a.xls') 不行,要 File.open('public/upload/a.xls') apache 下 File.open('upload/a.xls') 正常 为什么?