splinter123
splinter123

Reputation: 1203

Nginx server name issue

In my Nginx configuration, I would like to keep one service to be accessible with http, while all the others should be accessed through https, and forced to ssl when trying to connect with http. This is my config:

server{
    server_name localhost;
    listen 80;
    proxy_http_version 1.1;

    location /services/ {
        proxy_pass http://localhost:47440/;
    }

    listen / {
        rewrite     ^   https://$server_name$request_uri? permanent;

}


server{
    server_name localhost_ssl;
    listen 443 ssl;
    ssl_certificate /etc/nginx/ssl/mycert.crt;
    ssl_certificate_key /etc/nginx/ssl/mycert.key;
    proxy_http_version 1.1;

    location /db/ {
        proxy_pass http://localhost_ssl:8084/;
    }
}

My problem is that when trying to reload I get this error:

 host not found in upstream "localhost_ssl" in /etc/nginx/nginx.conf:46

Any idea of why this happens?

Upvotes: 1

Views: 3712

Answers (2)

niteshnarayanlal
niteshnarayanlal

Reputation: 553

It seems your DNS resolver is failing for some reason. Try adding: options single-request

to /etc/resolv.conf This causes IPv6/v4 lookups to be done sequentially.

Upvotes: 1

Lich4r
Lich4r

Reputation: 1383

You got this error because nginx can't find the host "localhost_ssl". Indeed it doesn't exist unless you specify it with upstream directive (or in the hosts file I think). You should set it to proxy_pass http://localhost:8084/; assuming your service is really listening on 127.0.0.1:8084.

Furthermore you may want to replace listen / { with location / {.

UPDATE : If you access your server with your IP (you don't have a domain name), then you can remove server_name directive :

server {
  listen 80;
  proxy_http_version 1.1;

  location /services {
      proxy_pass http://localhost:47440/;
      proxy_set_header Host $host;
  }

  location / {
      return 301 https://$host$request_uri?; # Replace $server_name by $host
  }


server {
  listen 443 ssl;
  ssl_certificate /etc/nginx/ssl/mycert.crt;
  ssl_certificate_key /etc/nginx/ssl/mycert.key;
  proxy_http_version 1.1;

  location /db {
      proxy_pass http://localhost:8084/;
      proxy_set_header Host $host;
  }
}

That config redirects requests received on port 80 to 443 if they don't match location /services. Requests received on port 443 are proxied if they match location /db.

But is this what you really want to achieve ? I mean a request on port 443 for /test would not match any location as there is only /db.

Upvotes: 0

Related Questions