Nginx two server blocks listen on 80 and 81 not working

510 views Asked by At

Inside /etc/nginx/sites-available I have these two three files:

default

server {
    listen 80 default_server;
    listen [::]:80 ipv6only=on default_server;

    root /var/www/html;
    index index.html index.htm index.nginx-debian.html;
    server_name _;

    location / {
        # First attempt to serve request as file, then
        # as directory, then fall back to displaying a 404.
        try_files $uri $uri/ =404;
    }
}
include /etc/nginx/sites-available/ios_conciseph.conf;
include /etc/nginx/sites-available/conciseph.conf;

conciseph.conf

server{
    listen 80;
    server_name staging.app.conciseph.com;
    passenger_enabled on;
    passenger_spawn_method direct;
    passenger_min_instances 1;
    passenger_app_env staging;
    #passenger_ruby /usr/local/rvm/rubies/ruby-2.7.0/bin/ruby;
    passenger_ruby /usr/local/rvm/gems/ruby-2.7.0/wrappers/ruby;
    root /var/www/conciseph/public;
    location ~ ^/assets/ {
        gzip_static on;
        add_header Cache-Control public;
        expires 4w;
        gzip on;
        gzip_vary on;
        gzip_proxied any;
        gzip_disable "MSIE [1-6]\.";
        gzip_comp_level 6;
        gzip_types application/x-javascript text/css text/html image/x-icon image/png image/jpeg image/gif image/jpg;
    }
    location / {
        root /var/www/conciseph/public;
        autoindex off;
        #proxy_cache conciseph;
        proxy_cache_use_stale error timeout http_500 http_502 http_503 http_504;
        proxy_cache_revalidate on;
        proxy_cache_min_uses 2;
        proxy_cache_lock on;
        passenger_enabled on;
        log_not_found off;
        if ($http_user_agent ~ (libwww|Wget|LWP|damnBot|BBBike|java|spider|crawl) ) {
            return 403;
        }
    }
    location ~* \.(?:ico|css|js|gif|jpe?g|png)$ {
      expires 30d;
      add_header Pragma public;
      add_header Cache-Control "public";
    }

}

ios_conciseph.conf

server{
    listen 81;
    server_name staging.app.conciseph.com:81;
    passenger_enabled on;
    passenger_spawn_method direct;
    passenger_min_instances 1;
    passenger_app_env staging;
    #passenger_ruby /usr/local/rvm/rubies/ruby-2.7.0/bin/ruby;
    passenger_ruby /usr/local/rvm/gems/ruby-2.7.0/wrappers/ruby;
    root /var/www/ios_conciseph/conciseph/public;
    location ~ ^/assets/ {
        gzip_static on;
        add_header Cache-Control public;
        expires 4w;
        gzip on;
        gzip_vary on;
        gzip_proxied any;
        gzip_disable "MSIE [1-6]\.";
        gzip_comp_level 6;
        gzip_types application/x-javascript text/css text/html image/x-icon image/png image/jpeg image/gif image/jpg;
    }
    location / {
        root /var/www/ios_conciseph/conciseph/public;
        autoindex off;
#        proxy_cache conciseph;
        proxy_cache_use_stale error timeout http_500 http_502 http_503 http_504;
        proxy_cache_revalidate on;
        proxy_cache_min_uses 2;
        proxy_cache_lock on;
        passenger_enabled on;
        log_not_found off;
        if ($http_user_agent ~ (libwww|Wget|LWP|damnBot|BBBike|java|spider|crawl) ) {
            return 403;
        }
    }
    location ~* \.(?:ico|css|js|gif|jpe?g|png)$ {
      expires 30d;
      add_header Pragma public;
      add_header Cache-Control "public";
    }

}

when I hit staging.app.conciseph.com/users/sign_up it gives me expected response but when I hit staging.app.conciseph.com:81/users/sign_up it keeps on loading and eventually raises error 'This site can not be reached'

I can not even find any request being processed in the /var/log/nginx/access.log

I want to run same app using two diferent urls.

folder one is inside : /var/www/conciseph & folder two path: /var/www/ios_conciseph/conciseph

1

There are 1 answers

3
Sascha S. On

I noticed you included the port in the server name filed. Try removing the port an re-check as depending on the server name determined by nginx, you might not have the port included (see http://nginx.org/en/docs/http/server_names.html).

As you can see in the Host field, there is usually no port included, so you have no chance to match it to a server name containing a port:

$ curl -v staging.app.conciseph.com
$ curl -v staging.app.conciseph.com:80
$ curl -v staging.app.conciseph.com:81

$ curl -v staging.app.conciseph.com
...
> GET / HTTP/1.1
> Host: staging.app.conciseph.com
> User-Agent: curl/7.81.0
> Accept: */*
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 404 Not Found
< Content-Type: text/html; charset=UTF-8
< Content-Length: 1722
< Connection: keep-alive
< Status: 404 Not Found
...

As you cannot see any requests in the access log, you might want to check your firewall and have a look in the nginx error log.