1

I have a droplet on Digital Ocean, that I am using to host a site and an API for that site.

I would like:

Here's my /etc/nginx/nginx.conf file:

http {
        include /etc/nginx/mime.types;
        default_type application/octet-stream;

        log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

        access_log  /var/log/nginx/access.log  main;
        error_log /var/log/nginx/http-error.log;

        sendfile            on;
        tcp_nopush          on;
        tcp_nodelay         on;
        keepalive_timeout   65;
        types_hash_max_size 2048;

        server { 
                server_name example.com; # managed by Certbot 

                listen [::]:443 ssl ipv6only=on; # managed by Certbot 
                listen 443 ssl; # managed by Certbot 

                # SSL settings

                ssl_certificate /path/to/file.pem; # managed by Certbot 
                ssl_certificate_key /path/to/file.pem; # managed by Certbot 
                include /path/to/file.conf; # managed by Certbot 
                ssl_dhparam /path/to/file.pem; # managed by Certbot 

                proxy_http_version 1.1; 
                proxy_set_header Connection 'upgrade';
                proxy_set_header Host $http_host; 
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header X-Forwarded-For $remote_addr; 
                proxy_set_header X-Real-IP $remote_addr; 

                proxy_cache_bypass $http_upgrade;

                proxy_redirect off;

                # Routes

                location /api/ { 
                        proxy_pass http://127.0.0.1:3000/; 
                } 

                location / { 
                        root /usr/share/nginx/html; 
                } 

                error_page 404 /404.html; 
                location = /40x.html {}

                error_page 500 502 503 504 /50x.html; 
                location = /50x.html {}
        } 

        server { 
                if ($host = example.com) { 
                        return 301 https://$host$request_uri; 
                } # managed by Certbot 

                listen       80 ; 
                listen       [::]:80 ; 
                server_name example.com; 
                return 404; # managed by Certbot 
        } 
} 

Serving the static html files works great, but the https://example.com/api/ returns a 502: Bad Gateway error. I don't understand what I am doing wrong... any help would be appreciated. Thank you.

2 Answers 2

1

Turns out my config was totally fine. I just need to enable networking on the Droplet. I used this post to do so. Thanks, everyone!

In short:

setsebool httpd_can_network_connect on
Sign up to request clarification or add additional context in comments.

Comments

0

location / {
    proxy_set_header  X-Real-IP  $remote_addr;
    proxy_set_header  Host       $http_host;
    proxy_pass        http://127.0.0.1:3000;
  }

This normally does not disappoint me. Please try.

1 Comment

These headers are already set under the server directive, and the proxy_pass is set under the location /api/ directive. I want / to serve static html that is in the directory above. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.