Fighting location blocks in nginx configuration

I got a new slicehost snippet, in order to play around and learn nginx and more about deployment in general. I installed there a ruby ​​application (which I will call app1) that uses the passenger. I made this default application for use on this server with the following server block in my nginx configuration:

server { listen 80; server_name <my server ip>; root <path to app1 public folder>; passenger_enabled on; } 

It works great. However, I want to try several different applications on this snippet, and therefore thought that I would configure it like this:

Http: /// app1

Http: /// app2

etc .. I thought I could do this by adding a location block and moving the specific things of application 1 into it like this:

 server { listen 80; server_name <my server ip>; location ^~ /app1 { root <path to app1 public folder>; passenger_enabled on; } } 

However, by doing this (and, of course, by rebooting nginx), switching to a simple IP address gives the message "welcome to nginx" (which I expect). But, going to / app 1, an error message is displayed:

 404 Not Found The requested URL /app1 was not found on this server. 

This is different from the error message I get when I go to another path on this ip, for example / foo:

 404 Not Found nginx/0.8.53 

So, as nginx knows about this place, but I did not configure it correctly. Can someone set me up right? Should I configure different server blocks instead of using locations? I am sure it is simple, but cannot solve it.

Greetings, max

+4
source share
5 answers

What you need is virtual name hosting. The idea is that each domain is hosted on a single IP address, and nginx selects a virtual host for service based on the Host: header in the HTTP request sent by the browser.

To use shared name hosting, use the domain you want to use instead of the server IP address for the server_name directive.

 server { listen 80; server_name app1.com; location / { root /srv/http/app1/public; passenger_enabled on; } } 

Then, to place more applications in one window, simply declare a separate server { } block for each of them.

 server { listen 80; server_name app2.com; location / { root /srv/http/app2/public; passenger_enabled on; } } 

I use a unicorn instead of a passenger, but part of the host structure is the same for any backend.

Global nginx configuration (which in itself has nothing): https://github.com/benhoskings/babushka-deps/blob/master/nginx/nginx.conf.erb

Template wrapper for each virtual host: https://github.com/benhoskings/babushka-deps/blob/master/nginx/vhost.conf.erb

Unicorn virtual host details: https://github.com/benhoskings/babushka-deps/blob/master/nginx/unicorn_vhost.common.erb

+3
source

I don’t see the real problem here,
in order for you to understand this you need to look at the nginx log files on most systems:
/ Var / Magazine / Nginx /
and open the corresponding access file here (maybe error.log) there you can see what url nginx was trying to get, and why it didn’t work.
What I really think is happening is that you have the wrong root path, maybe it should be an alias instead, if you proxy the connection to another application, it can get the "app1" word in the URL instead of the direct one.

Please, try:

 server { listen 80; server_name <my server ip>; location /app1 { alias <path to app1 public folder>; passenger_enabled on; } } 

and see how it works, and try looking at the logs first to really determine what the problem is.

+3
source

I think this is just a small syntax problem:

  location ~ ^/app1 { ... 

should work, or a little more efficiently:

  location = /app1 { ... 
+2
source

One of the problems is that your Rails application is probably not designed to run from a subdirectory. The passenger has a directive that will correct this:

 passenger_base_uri /app1; 

However, running Rails applications in subdirectories is somewhat non-standard. If possible, setting up subdomains using nginx virtual hosts might be the best option.

+2
source

It seems that you want to host more applications on the same server with the base uri. Try the following:

 root /srv/http/; passenger_base_uri /app_1; passenger_base_uri /app_2 

Also under / srv / http, create 2 symbolic links:

 ln -s /srv/http/app_1 /srv/http/app1/public ln -s /srv/http/app_2 /srv/http/app2/public 

Access to app1 can be obtained at: http://domain.com/app_1 .

Read more here: http://www.modrails.com/documentation/Users%20guide%20Nginx.html#deploying_rack_to_sub_uri

+2
source

Source: https://habr.com/ru/post/1342161/


All Articles