How to rewrite correctly on Nginx?

Asked

Viewed 1,124 times

1

I’m having trouble setting up Nginx as a reverse proxy in an application. It’s a Rails application that runs on Unicorn.

In case, I have the mastery http://www.meudominio.com.br. Instead of creating a subdomain, I would like http://www.meudominio.com.br/meuprojeto/ was the root url and directed the requests to my application. Looking in the English OS I saw some possible solutions. I tested some, and following the one that came closest to working, my configuration file got like this:

upstream meuprojeto {
    server unix:/var/www/apps/meuprojeto/shared/tmp/sockets/meuprojeto.sock;
}

server {
    listen 80;
        server_name meuprojeto.com.br max_fails=0;
        index index.html;
        root /var/www/apps/meu_projeto/current/public;
        error_page 500 502 503 504 /500.html;
        client_max_body_size 4G;
        keepalive_timeout 10;

        error_log /var/log/nginx/meu_projeto.log debug;

        location /meuprojeto {
            rewrite ^/meuprojeto/(.*)$ /$1;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Host $http_host;
            proxy_redirect off;
            proxy_pass http://meuprojeto/;
        }   
}

With this configuration, when access http://www.meudominio.com.br/meuprojeto , the request is correctly directed to the home of my application, but all links stop working, since the generated url’s are like http://www.meudominio.com.br/sobre ,when they should be something like http://meudominio.com.br/meuprojeto/sobre. What to do to make it work? The configuration is in Nginx or should be done in Rails?

  • there is some reason to use rewrite, it would not be better to use the "Passenger"?

  • I don’t really understand these settings of Nginx... I used rewrite because I saw an answer from the English OS.

  • Yes, normal... but what I want to know is if you want to use rewrite for some reason, like customization, or you followed some tutorial that indicated this, because the answer I have in mind would not be using rewrite. Is using Linux?

  • I followed an answer that indicated this. There is no reason to use it. Yes, Ubuntu Server + Nginx + Unicorn.

  • I added an answer, if you fail let me know, because I will try to put a step by step complete!

2 answers

0

In order for your application’s links to work, you need to set in the application the configuration relative_url_root, as explained in Deploy to a subdirectory of Rails Guides.

0

Assuming that Unicorn and Rails are properly configured, it might be best to use try_files, thus:

server {
    listen 80;
    server_name localhost;

    root /var/www/apps/meu_projeto/current/public;

    try_files $uri/index.html $uri @app;

    location @app {
        proxy_pass http://meuprojeto;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_redirect off;
    }

    error_page 500 502 503 504 /500.html;
    client_max_body_size 4G;
    keepalive_timeout 10;
}

After editing, save the file and restart ngninx:

sudo service nginx restart

Browser other questions tagged

You are not signed in. Login or sign up in order to post.