4
votes

I'm noob in rails production environment. Can I configure nginx and unicorn to response on other (not 80) port? I don't need any domains, i need only ip and port. Here is nginx config to my application (80 port):

server {
    listen 80;
    server_name  localhost;
    root /home/my_app_folder/web-app/public;
    client_max_body_size 32m;
    location / {
        try_files  $uri @unicorn;
    }
    location @unicorn {
        proxy_set_header  Client-Ip $remote_addr;
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header  Host $host;
        proxy_pass  http://unix:/home/my_app_folder/web-app/tmp/sockets/unicorn.sock;
    }
}

My application work fine with 80 port. If I change 80 to 3000 for example, like here:

server {
    listen 3000;
    server_name  localhost;
    root /home/my_app_folder/web-app/public;
    client_max_body_size 32m;
    location / {
        try_files  $uri @unicorn;
    }
    location @unicorn {
        proxy_set_header  Client-Ip $remote_addr;
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header  Host $host;
        proxy_pass  http://unix:/home/my_app_folder/web-app/tmp/sockets/unicorn.sock;
    }
}

I get a problem. Everything work fine, I can go to http://my_ip:3000 but Rails' url helpers (for example root_url, ect.) are without port (http://my_ip).

What can I do to see url helpers with port? Is there a way to do this in the configuration files of Nginx or maybe Unicorn? After all, when I run locally Webrick, it has the form of URL http://localhost:3000 and every *_url helper return url with the port 3000. I need to could change a few config files, and transfer the application to another port.

Thanks.

1
hey @kasper375, did you solve this?caarlos0

1 Answers

6
votes

Try putting something like this in your application controller:

default_url_options[:port] = 3000 if Rails.env.production?

or, depending on what version of Rails you are using:

Rails.application.routes.default_url_options[:port]= 3000 if Rails.env.production?

Credit - Dylan's answer at:

default_url_options and rails 3