Skip to content

Instantly share code, notes, and snippets.

@hipertracker
Created October 20, 2009 12:05
Show Gist options
  • Select an option

  • Save hipertracker/214210 to your computer and use it in GitHub Desktop.

Select an option

Save hipertracker/214210 to your computer and use it in GitHub Desktop.

Revisions

  1. hipertracker revised this gist Oct 22, 2009. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -40,9 +40,9 @@ because the same rule for APACHE WORKS FINE.

    SOLUTION

    After several hours of testing different Nginx settings, I found the final solution. To duplicate Apache behavior, Nginx need few additional HTTP headers:
    To duplicate Apache behavior, Nginx need few additional HTTP headers:

    erver {
    server {
    listen myhost:80;
    server_name myhost;
    location / {
  2. hipertracker revised this gist Oct 22, 2009. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -2,6 +2,7 @@
    127.0.0.1 myhost jboss_server

    NGINX:

    server {
    listen myhost:80;
    server_name myhost;
  3. hipertracker revised this gist Oct 22, 2009. 1 changed file with 1 addition and 2 deletions.
    3 changes: 1 addition & 2 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -53,5 +53,4 @@ erver {
    }
    }

    It was also added to http://wiki.nginx.org/NginxLikeApache
    It wasadded also to http://wiki.nginx.org/NginxLikeApache
    (It was also added to http://wiki.nginx.org/NginxLikeApache)
  4. hipertracker revised this gist Oct 22, 2009. 1 changed file with 19 additions and 0 deletions.
    19 changes: 19 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -32,7 +32,26 @@ because the same rule for APACHE WORKS FINE.

    <VirtualHost *:80>
    ServerName myhost
    DocumentRoot /path/to/rails1/public
    ProxyPass / http://rails1:8080/
    ProxyPassReverse / http://rails1:8080/
    </VirtualHost>

    SOLUTION

    After several hours of testing different Nginx settings, I found the final solution. To duplicate Apache behavior, Nginx need few additional HTTP headers:

    erver {
    listen myhost:80;
    server_name myhost;
    location / {
    root /path/to/rails1/public;
    proxy_set_header X-Forwarded-Host $host;
    proxy_set_header X-Forwarded-Server $host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_pass http://rails1:8080;
    }
    }

    It was also added to http://wiki.nginx.org/NginxLikeApache
    It wasadded also to http://wiki.nginx.org/NginxLikeApache
  5. hipertracker revised this gist Oct 21, 2009. 1 changed file with 15 additions and 21 deletions.
    36 changes: 15 additions & 21 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -1,27 +1,15 @@
    /etc/hosts:

    127.0.0.2 homepage jboss_server
    127.0.0.4 mydomain
    127.0.0.1 myhost jboss_server

    NGINX:

    /etc/nginx/sites-enabled/mydomain.conf:

    server {
    listen mydomain:80;
    server_name mydomain;
    listen myhost:80;
    server_name myhost;
    location / {
    if (-f $request_filename) {
    access_log off;
    expires 30d;
    break;
    }
    proxy_redirect off;
    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_set_header "ENABLE_X_ACCEL_REDIRECT" "true";
    proxy_pass http://homepage:8080;
    proxy_pass http://rails1:8080;
    }
    }

    @@ -33,12 +21,18 @@ $ $TORQUEBOX_HOME/jboss/run.sh -c web -b jboss_server
    $ elinks http://jboss_server:8080
    returns error404, but it is fine because I removed root.war file

    $ elinks http://homepage:8080
    $ elinks http://rails1:8080
    works fine, it opens Ruby on Rails application

    $ elinks http://mydomain
    returns error404. It looks like it opens http://127.0.0.2:8080
    or http://jboss_server:8080.. (JBoss distinguish between http://homepage:8080
    and http://127.0.0.2:8080 even if it is the same IP).
    $ elinks http://myhost
    returns error404.

    It looks like Nginx has some issues with reverse proxy
    because the same rule for APACHE WORKS FINE.

    <VirtualHost *:80>
    ServerName myhost
    ProxyPass / http://rails1:8080/
    ProxyPassReverse / http://rails1:8080/
    </VirtualHost>

  6. hipertracker revised this gist Oct 20, 2009. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -28,7 +28,7 @@ server {
    TEST

    Starting JBoss:
    $ TORQUEBOX_HOME/jboss/run.sh -c web -b jboss_server
    $ $TORQUEBOX_HOME/jboss/run.sh -c web -b jboss_server

    $ elinks http://jboss_server:8080
    returns error404, but it is fine because I removed root.war file
  7. hipertracker revised this gist Oct 20, 2009. 1 changed file with 0 additions and 10 deletions.
    10 changes: 0 additions & 10 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -42,13 +42,3 @@ or http://jboss_server:8080.. (JBoss distinguish between http://homepage:8080
    and http://127.0.0.2:8080 even if it is the same IP).


    SOLUTION!

    Change nginx settings. Replace

    proxy_set_header Host $http_host;

    with

    proxy_set_header Host homepage;

  8. hipertracker revised this gist Oct 20, 2009. 1 changed file with 12 additions and 0 deletions.
    12 changes: 12 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -40,3 +40,15 @@ $ elinks http://mydomain
    returns error404. It looks like it opens http://127.0.0.2:8080
    or http://jboss_server:8080.. (JBoss distinguish between http://homepage:8080
    and http://127.0.0.2:8080 even if it is the same IP).


    SOLUTION!

    Change nginx settings. Replace

    proxy_set_header Host $http_host;

    with

    proxy_set_header Host homepage;

  9. hipertracker created this gist Oct 20, 2009.
    42 changes: 42 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,42 @@
    /etc/hosts:

    127.0.0.2 homepage jboss_server
    127.0.0.4 mydomain

    NGINX:

    /etc/nginx/sites-enabled/mydomain.conf:

    server {
    listen mydomain:80;
    server_name mydomain;
    location / {
    if (-f $request_filename) {
    access_log off;
    expires 30d;
    break;
    }
    proxy_redirect off;
    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_set_header "ENABLE_X_ACCEL_REDIRECT" "true";
    proxy_pass http://homepage:8080;
    }
    }

    TEST

    Starting JBoss:
    $ TORQUEBOX_HOME/jboss/run.sh -c web -b jboss_server

    $ elinks http://jboss_server:8080
    returns error404, but it is fine because I removed root.war file

    $ elinks http://homepage:8080
    works fine, it opens Ruby on Rails application

    $ elinks http://mydomain
    returns error404. It looks like it opens http://127.0.0.2:8080
    or http://jboss_server:8080.. (JBoss distinguish between http://homepage:8080
    and http://127.0.0.2:8080 even if it is the same IP).