ngnix+proxy+mongrel => real host

Igor Sysoev is at rambler-co.ru
Fri Apr 27 12:15:18 MSD 2007


On Fri, Apr 27, 2007 at 10:02:43AM +0200, Julien Biard wrote:

> Hi,
> 
> 
> I want to use ngnix + mongrel in proxy with different URLs, e.g 
> site1.com, site2.fr... and I need to get the real host in my application 
> (with request.host) and not the host used in upstream declaration.
> Any idea ?
> 
> 
> In this case, I have mongrel as host instead of site1.com or site2.fr :/

Set these

      proxy_set_header   Host             $host;
      proxy_set_header   X-Real-IP        $remote_addr;
      proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;

at http level, so they will be inherited (if not overriden by
any proxy_set_header) to all servers and locations.

Then all request to the mongrel upstream will have a client "Host".

> %<%<%<%<%<%<%<%<%<%<%<%<%<%<%<%<%<%<%<%<%<%<%<%<%<%<%<%<%<%<%<%<%<%<%<%<%<%<%<
> /etc/nginx/ngnix.conf
> 
> user nginx nginx;
> worker_processes 2;
> 
> error_log /var/log/nginx/error_log info;
> 
> events {
>   worker_connections 1024;
>     use epoll;
> }
> 
> http {
>     include        /etc/nginx/mime.types;
>     default_type    application/octet-stream;
> 
>     log_format main
>         '$remote_addr - $remote_user [$time_local] '
>     '"$request" $status $bytes_sent '
>         '"$http_referer" "$http_user_agent" '
>         '"$gzip_ratio"';
> 
>     client_header_timeout    10m;
>     client_body_timeout    10m;
>     send_timeout 10m;
> 
>     connection_pool_size 256;
>     client_header_buffer_size    1k;
>     large_client_header_buffers    4 2k;
>     request_pool_size    4k;
> 
>     output_buffers 1 32k;
>     postpone_output    1460;
> 
>     sendfile on;
>     tcp_nopush on;
>     tcp_nodelay    on;
> 
>     keepalive_timeout    75 20;
> 
>     ignore_invalid_headers on;
> 
>     index index.html;
> 
>   gzip on;
>   gzip_min_length 1100;
>   gzip_buffers 4 8k;
>   gzip_types text/plain;
> 
>     client_max_body_size 11000k;
> 
>   upstream mongrel {
>         server 127.0.0.1:8000;
>         server 127.0.0.1:8001;
>         server 127.0.0.1:8002;
>         server 127.0.0.1:8003;
>   }
> 
> 
>     # ruby/mongrel
>     server {
>         listen 80;
>         server_name site1.com;
> 
>         error_log    /var/log/nginx/site1.com_error.log;
>         include /etc/nginx/include;
>     }
> 
>     server {
>         listen 80;
>         server_name site2.fr;
>         error_log    /var/log/nginx/site2.fr_error.log;
>         include /etc/nginx/include;
> 
>     }
> 
> }
> 
> %<%<%<%<%<%<%<%<%<%<%<%<%<%<%<%<%<%<%<%<%<%<%<%<%<%<%<%<%<%<%<%<%<%<%<%<%<%<%<
> /etc/nginx/include :
> 
> 
>         listen 80;
> 
>     root /home/www/app/current/public;
> 
>         access_log  off;
>     rewrite_log on;
> 
>     # / -> first search for local index.html then go to mongrel
>     location ~ ^/$ {
>             if (-f /index.html){
>                 rewrite (.*) /index.html last;
>             }
>             proxy_pass  http://mongrel;
>     }
> 
>     # rail caching: searching first for $action.html local pages
>     location / {
>             if (!-f $request_filename.html) {
>             proxy_pass  http://mongrel;
>             }
>             rewrite (.*) $1.html last;
>     }
> 
>     # serve static files directly
>     location ~ .html {
>             root /home/www/app/current/public;
>     }
> 
>     location ~* 
> ^.+\.(jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js|mov)$ 
> {
>             root /home/www/app/current/public;
>     }
> 
>     # resend everything else to mongrel
>     location / {
>             proxy_pass  http://mongrel;
>             proxy_redirect     off;
>             proxy_set_header        Host $http_host;
>             proxy_set_header   X-Real-IP        $remote_addr;
>             proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
>     }
> 
> %<%<%<%<%<%<%<%<%<%<%<%<%<%<%<%<%<%<%<%<%<%<%<%<%<%<%<%<%<%<%<%<%<%<%<%<%<%<%<
> 
> 

-- 
Igor Sysoev
http://sysoev.ru/en/





More information about the nginx mailing list