My React app won't load after modifying nginx proxy_pass settings
P粉427877676
P粉427877676 2024-04-05 11:53:10
0
1
436

I have React app Node JS backend and nginx. I have obtained the certificate and installed it via Certbot.

My application makes get and post requests, but for this I need to set the proxy_pass setting.

My server block file:

server {

        root /var/www/nikolsoborsocial/html;
        index index.html index.htm index.nginx-debian.html;

        server_name nikolsoborsocial nikolsoborsocial.org
                                     www.nikolsoborsocial.org;

        location / {
                try_files $uri $uri/ =404;
                               
        }
        
        


    listen [::]:443 ssl ipv6only=on; # managed by Certbot
    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/nikolsoborsocial.org/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/nikolsoborsocial.org/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.nikolsoborsocial.org) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


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


        listen 80;
        listen [::]:80;

        server_name nikolsoborsocial nikolsoborsocial.org
                                     www.nikolsoborsocial.org;

        
        return 404; # managed by Certbot


}

Do I need to add proxy_pass settings?

proxy_pass http://localhost:3000;
include proxy_params;

If I put this into a 433 server location, insert try_files $uri $uri/ =404; My React app won't load and I get a Cannot GET in the browser / mistake.

P粉427877676
P粉427877676

reply all(1)
P粉481815897

You set up the nginx configuration file to serve React files in the "location/" server block.

So if you try to add the proxy_pass setting in the "location/" block, it will override the code that serves the react file. Nginx will proxy the request to the backend server running on localhost:3000.

How to solve this problem?

You must provide a file in the backend server for this request, or add a new location block.

This is an example of adding a new location block

server {
    root /var/www/nikolsoborsocial/html;
    index index.html index.htm index.nginx-debian.html;

    server_name nikolsoborsocial nikolsoborsocial.org www.nikolsoborsocial.org;

    location / {
        try_files $uri $uri/ =404;
    }

    # Proxy Pass settings
    location /app {
        proxy_pass http://localhost:3000;
        include proxy_params;
    }

    # SSL configuration
    listen [::]:443 ssl ipv6only=on;
    listen 443 ssl;
    ssl_certificate /etc/letsencrypt/live/nikolsoborsocial.org/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/nikolsoborsocial.org/privkey.pem;
    include /etc/letsencrypt/options-ssl-nginx.conf;
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;
}
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template