Search code examples
httpnginxhttpsproxyhttp-proxy

Catch specific server on nginx


My nginx file looks like:

server {
    listen 443 ssl;
    server_name local.awesome.com;

    ssl_certificate /opt/certs/local.awesome.com.crt;
    ssl_certificate_key /opt/certs/local.awesome.com.key;

    location / {
        root /var/www/awesome.com/public_html/;
        index index.html;
    }
}
server {
    listen 443 ssl;
    server_name api.local.awesome.com;

    ssl_certificate /opt/certs/local.awesome.com.crt;
    ssl_certificate_key /opt/certs/local.awesome.com.key;

    root /var/www/api.awesome.com/public_html/;

    # Known locations for static resources
    location /resources/ {
    }

    # Process all other requests via JS in index.html
    location / {
        rewrite .* /index.html;
        break;
    }

    location /api {
        rewrite "^/api/(.*)$" /$1 break;
        proxy_pass http://api:8001;
    }
}

If I query something similar to:

GET https://api.local.awesome.com/api/

This works fine.

I decided to make this accessible globally to share some data.

I'm trying to request:

GET https://192.168.1.3:443/api/

But this doesn't work. It returns HTTP/1.1 404 Not Found.

This request returns 403 Forbidden:

GET https://192.168.1.3:443/

It looks like everything is with authorization here, but I hope that previous request should return something different from Not Found.

What is wrong here and how to replace:

GET https://api.local.awesome.com/api/

with

GET http://192.168.1.3:443/api/

If schema or port are different it's not critical for me.

Any suggestions?

UPDATE:

curl -v http://192.168.1.3/api/
*   Trying 192.168.1.3...
* TCP_NODELAY set
* Connected to 192.168.1.3 (192.168.1.3) port 80 (#0)
> GET /api/ HTTP/1.1
> Host: 192.168.1.3
> User-Agent: curl/7.54.0
> Accept: */*
> 
< HTTP/1.1 301 Moved Permanently
< Server: nginx/1.13.3
< Date: Fri, 01 Sep 2017 18:55:05 GMT
< Content-Type: text/html
< Content-Length: 185
< Connection: keep-alive
< Location: https://192.168.1.3/api/
< 
<html>
<head><title>301 Moved Permanently</title></head>
<body bgcolor="white">
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx/1.13.3</center>
</body>
</html>
* Connection #0 to host 192.168.1.3 left intact

Solution

  • Change below

    listen 443 ssl;
    server_name api.local.awesome.com;
    

    to

    listen 443 ssl;
    listen 80;
    server_name api.local.awesome.com _;
    

    or

    listen 443 ssl;
    listen 80;
    server_name api.local.awesome.com 192.168.1.3;
    

    This would allow you to access it using http://192.168.1.3/api/