Getting the URLs right when proxying to apache and mongrel

marc gmane at
Sun Sep 2 18:32:25 MSD 2007


I'm testing nginx by putting it in front of an existing Apache server, 
and also using it to serve rails via mongrel.

This works okay, mostly, but for a couple of things:

1. When I proxy to Apache (on port 81) the URLs are written showing the 
port, e.g.


I'd like not not to see the port number, if possible,

I tried using

    upstream apache {
      server mote:81;


    location / {
        proxy_pass   http://apache;

but that resulted in URLs of the form: http://apache/...

2. The url required to access the rails app fails without the trailing 
slash. So,

works, but the following doesn't


I'd like the latter to work, if possible,

I've added my conf file below for reference.


user www-data;
worker_processes  1;

error_log  /var/log/nginx/error.log;
pid        /var/run/;

events {
    worker_connections  1024;

http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;
    access_log	/var/log/nginx/access.log;
    sendfile        on;
    keepalive_timeout  65;
    tcp_nodelay        on;
    gzip  on;
    gzip_min_length  1100;
    gzip_buffers     4 8k;
    gzip_types       text/plain;

    upstream mongrel {

    server {
        listen       80;
        server_name  mote;

        access_log  /var/log/nginx/localhost.access.log;
        root   /var/www;
        index  index.html index.htm;

        location / {
            proxy_pass   http://mote:81;

        location ^~ /rails {

          proxy_set_header  X-Real-IP  $remote_addr;
          proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
          proxy_set_header Host $http_host;
          proxy_redirect false;

          if (!-f $request_filename) {
          root /home/marc/rails/mrctest/public;
            rewrite ^/rails(.*) $1;
            proxy_pass http://mongrel;

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   /var/www/nginx-default;

More information about the nginx mailing list