Report this

What is the reason for this report?

Proxying nginx to port 1337 with Strapi

Posted on July 18, 2020

Trying to make a proxy from domain: api.example.com to api.example.com:1337. The proxy is working sort of. If I use port 3000 (which is my main site) it all works, just not on 1337.

I have initialised the strapi-app with “yarn build”, and using pm2 to continuing run Node. Also making sure that port 1337 is allowed in the firewall.

Can anyone possible guess what the issue might be? I have tried almost everything I can think of :-)

This is my nginx configuration:

server {
    index index.html;
    server_name api.example.com;

    location / {
        proxy_pass http://api.example.com:1337;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }
}

My ufw firewall settings:

To                         Action      From
--                         ------      ----
1337/tcp                   ALLOW       Anywhere
1337                       ALLOW       Anywhere
1337/tcp (v6)              ALLOW       Anywhere (v6)
1337 (v6)                  ALLOW       Anywhere (v6)

Strapi-app structure:

someuser@someuser-website:~/api$ ls
README.md  api  build  config  extensions  favicon.ico  node_modules  package.json  public  yarn.lock


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Hi there @soduno,

The Nginx configuration actually looks correct. What is the exact error that you get when you visit your API?

Also what I could suggest is making sure that Strapi is listening on the correct port, to do that run the following command:

  1. sudo netstat -plant

Feel free to share the output here.

On another note, what I could also suggest is checking your Nginx error log with the following command:

  1. tail -100 /var/log/nginx/error.log

Regards, Bobby

Your Nginx configuration and UFW firewall settings look generally correct for proxying requests to a service running on port 1337. If you’re having trouble accessing your Strapi application through the Nginx proxy, there are several possible issues to consider:

1. Check Strapi Application

  • Ensure that your Strapi application is running correctly on port 1337. You can test it by accessing http://api.example.com:1337 directly (if this port is publicly accessible) or by accessing it from the server itself using curl http://localhost:1337.

2. Nginx Configuration

  • In your proxy_pass, try using localhost or the server’s internal IP address instead of the domain name:
location / {
    proxy_pass http://localhost:1337;
    # Other directives...
}

Ensure that Nginx is properly reloaded after making configuration changes:

sudo nginx -t
sudo systemctl reload nginx

3. Firewall Settings

  • Verify that your firewall allows traffic on port 1337. Based on your ufw settings, it seems fine, but it’s good to double-check:
sudo ufw status

4. DNS Configuration

  • Ensure that api.example.com resolves to the correct IP address where your Nginx server is hosted. You can check this with ping or dig commands.

5. Strapi Server Configuration

  • Verify the Strapi server configuration, ensuring it’s set to listen on the correct port and is not bound to localhost or 127.0.0.1 if you’re accessing it from an external server.

6. Check for Errors

  • Look at the Nginx error logs for any relevant messages:
sudo tail -f /var/log/nginx/error.log
  • Similarly, check your Strapi application logs for any errors that might indicate connection issues.

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.