I followed the tutorials to setup Nginx and deploy a Node.js/create-react-app. However, there seems to be a problem with my set up that I am unable to figure out. When I navigate to my app at “myapp.com”, it shows the DO hello.js file. That makes my think that my Nginx configuration is wrong.

I have also set up SSL for my app with Let’s Encrypt. Not sure if that’s pertinent information.

Here is my /etc/nginx/sites-available/default:

server {
        listen 80 default_server;
        listen [::]:80 default_server;

# SSL configuration
        #
        # listen 443 ssl default_server;
        # listen [::]:443 ssl default_server;
        #
        # Note: You should disable gzip for SSL traffic.
        # See: https://bugs.debian.org/773332
        #
        # Read up on ssl_ciphers to ensure a secure configuration.
        # See: https://bugs.debian.org/765782
        #
        # Self signed certs generated by the ssl-cert package
        # Don't use them in a production server!
        #

root /var/www/html;

        # Add index.php to the list if you are using PHP
        index index.html index.htm index.nginx-debian.html;

        server_name myapp.com www.myapp.com;

        location ^~ /assets/ {
                gzip_static on;
                expires 12h;
                add_header Cache-Control public;
  }

        location / {
                proxy_http_version 1.1;
                proxy_cache_bypass $http_upgrade;

                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection 'upgrade';
           proxy_set_header Host $host;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header X-Forwarded-Proto $scheme;

                proxy_pass http://4000;

        }
}

Here is my /etc/nginx/sites-available/myapp.com:

server {

        listen 80;
        listen [::]:80;
        root /var/www/myapp.com/html;
        index index.html index.htm index.nginx-debian.html;

        server_name myapp.com www.myapp.com;

        location / {
                proxy_pass http://localhost:3000;
        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;
        }

    listen [::]:443 ssl ipv6only=on; # managed by Certbot
    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/myapp.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/myapp.com/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

}

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


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


        listen 80;
        listen [::]:80;

        server_name myapp.com www.myapp.com;
    return 404; # managed by Certbot
}

Also, my app’s server.js listens on port 4000.

Any help would be greatly appreciated.

Thanks!

edited by MattIPv4

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.

×
1 answer

Hi there @donnyesq,

As far as I can see your proxy_pass rule is not complete, you need to update it from:

        proxy_pass http://4000;

To:

        proxy_pass http://localhost:4000;

After that run a config test:

  • sudo nginx -t

And if you get Syntax OK restart Nginx:

  • sudo systemctl restart nginx

Hope that this helps!
Regards,
Bobby

Submit an Answer