新手问题 请教一个 nginx 配置新手问题

cdredfox · 2012年10月24日 · 最后由 cxh116 回复于 2012年10月25日 · 6851 次阅读

问题是这样的:

我有一个域名:abc.com 我有一个网站只有二级域名:abc.demo.com 我对 abc.demo.com 的配置没有权限改,你可以想象成共享空间那样的。abc.demo.com 不支持绑定域名。即不对持 cname 之类的

我现在想把 abc.com 这个域名指向到 abc.demo.com 这个网站上。达到和 cname 一样的效果,不是做域名跳转,于是我采用 nginx 反向代理的方式来实现。

我现在找了一台机器,在这个机器上安装了 nginx,nginx 配置如下:

server { listen 80; server_name abc.com; location / { proxy_pass http://abc.demo.com/; proxy_redirect off; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } }

但是不知道为什么,我访问 Abc.com 这个域名是不能够反向代理到 abc.demo.com 这个网站上,报 404 错误,请问一下大家,我这个配置什么地方有错吗?谢谢了。

proxy_pass http://abc.demo.com/; 去掉"/"试试 proxy_pass http://abc.demo.com;

@cxh116 去掉试过,一样也是 404,并且查看 nginx 的 access.log 没有记录。

#2 楼 @cdredfox abc.com这个域名你已经改了 IP 指向?可以先在本地通过修改 hosts 来测试看看

这是我的反向代理配置,和你的差不多,你看一下你的 nginx error.log

proxy_temp_path   /tmp/proxy_temp_dir 1 2;
proxy_cache_path /tmp/proxy_cache_dir levels=1:2 keys_zone=nytimes:1m inactive=10m max_size=10m;
# the server directive is nginx's virtual host directive.
server {
# port to listen on. Can also be set to an IP:PORT
        listen 80;

# Set the max size for file uploads to 50Mb
        client_max_body_size 1M;

# sets the domain[s] that this vhost server requests for
# server_name www.[engineyard].com [engineyard].com;
        server_name cn_nytimes_feed.lvcake.com;

# doc root
        root /usr/share/nginx/html;

# vhost specific access log
        access_log  /home/outman/log/cnnytimesfeed_access.log  main;


        location / {
# needed to forward user's IP address to rails
#      proxy_set_header  X-Real-IP  $remote_addr;

# needed for HTTPS
#      proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
#      proxy_set_header Host $http_host;
                proxy_redirect off;
#      proxy_max_temp_file_size 0;


                proxy_pass http://cnnytimesfeed-mangege.rhcloud.com;

                proxy_connect_timeout      90;
                proxy_send_timeout         90;
                proxy_read_timeout         90;

                proxy_cache nytimes; 
                proxy_cache_valid 200 302 30m; 
                proxy_cache_valid 301 1d; 
                proxy_cache_valid any 30m; 

                proxy_buffer_size          4k;
                proxy_buffers              4 32k;
                proxy_busy_buffers_size    64k;
                proxy_temp_file_write_size 64k;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
                root   /usr/share/nginx/html;
        }
}


@cxh116 和你设置的没有什么区别,也没有 error 日志,刚咨询了一下 abc.demo.com,告知服务器目前不支持反向代理请求......

#4 楼 @cdredfox 它是没有办法禁用掉反向代理的,因为它其实就是一个普通请求 你先把proxy_set_header去掉,尽量把 http request header 改成用abc.demo.com一样 之后如果需要传 IP,把X-Real-IP改成一个非标准名称

#5 楼 @cxh116 非常谢谢,去掉后还真的就能访问了。再多问一句:X-Real-IP 在 rails 中一般在什么场景下会用到?

#6 楼 @cdredfox 拿访问者 ip 反向代理如果不加 X-Real-IP 和 X-Forwarded-For,到时request.remote_ip拿到的 ip 总是反向代理服务器的

你改成自定义的名称后可以这样改 request.headers["header name"]

#7 楼 @cxh116 哦,明白了,非常谢谢,不过我很奇怪的是为什么加上了 X-Real-IP 之后就直接 404 了,莫非是在服务端做了检测?

#7 楼 @cxh116 不知道 devise 记录的用户登录 IP 的取值的方法是否能更改,要看看它的文档了。不知道有人改过吗?

不知道在 ApplicationController 中加个 filter 然后在这个 filter 里面写上 request.remote_ip=request.headers["header name"] 这样是否可行?呵呵

#9 楼 @cdredfox 应该是服务器做了检测 你可以自己写一个 rack middleware,修改 request header

或直接修改 request.remote_ip 方法,因为看了一下 devise 调用的是request.remote_ip http://apidock.com/rails/ActionDispatch/Request/remote_ip 感觉只要在 devise 调用 remote_ip 方法前把 request 对象的@remote_ip变量改了就行了

#11 楼 @cxh116 嗯,我本来想在 ApplicationController 里面直接 request.remote_ip=request.headers["header name"] 的,不过好象不能直接访问 remote_ip 这个变量。

最终我在 app/helpers/application_helper.rb 中重新实现了 ActionDispatch::Request 的 remote_ip 方法。该问题算上圆满解决,呵呵,非常谢谢 @cxh116

#13 楼 @cdredfoxinstance_variable_set方法 request.instance_variable_set('@remote_ip', 'value') http://apidock.com/ruby/Object/instance_variable_set

#14 楼 @cxh116 嗯,这种方法也可以,不过我好象发现一个新的问题,就是 devise 登录之后跳转的时候,他貌似还是跳转到原来的 abc.demo.com 上去了,我估计 devise 跳转的时候是直接取的 header 中的 host 值,我尝试在在 nginx 中设置 host,设置后,设置后,就又不能跳转过去了,不知道你的站跳转的时候,有没有这个问题?

#15 楼 @cdredfox 这个和 IP 一样,你需要自定义的个 header,把 host 传过来 再重写 default_url_options 方法 http://stackoverflow.com/questions/2660172/how-do-i-set-default-host-for-url-helpers-in-rails

也许还要重写redirect_to方法,参数默认加上:host http://apidock.com/rails/ActionController/Redirecting/redirect_to

需要 登录 后方可回复, 如果你还没有账号请 注册新账号