0

I have configured the NGINX server configuration for the location of the application but I am getting the index.html data with 200 status instead of the original API response.

API response: enter image description here

enter image description here

The server configuration on the NGINX side is as follows:

server {
    listen 443 default_server;
    listen [::]:443 default_server;
    ssl on;

    server_name abc.global;
    root /var/www/html/build;

    location / {
            try_files $uri $uri/ /index.html;
    }

    location = /50x.html {
            alias /var/www/html/build;
    }

    error_page  404     /404.html;
    error_page  403     /403.html;

    # To allow POST on static pages
    error_page  405     =200 $uri;

    ssl_certificate /Path/abc.crt;
    ssl_certificate_key /Path/abc.key;

Can anyone please guide me, Whats the issue in the server configuration that I am getting this issue?

2
  • i dont see your api being proxy passed in /location block, why is it so?? Commented Sep 4, 2024 at 5:05
  • I do not have that much experience with this server configuration, Can you please guide me on the value of this proxy and how I can define it? Commented Sep 6, 2024 at 21:47

2 Answers 2

0

proxy_pass

An api needs a different settings compared with static web. You need to use proxy_pass. Check:

tips

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

Comments

0

change your NGINX config to following:

server {
    listen 443 default_server;
    listen [::]:443 default_server;
    ssl on;

    server_name abc.global;
    root /var/www/html/build;

    # Handle API requests separately
    location /api/ {
        proxy_pass http://localhost:3000;  # Adjust this to your API server's address and port
        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;
    }

    # Serve static files and fallback to index.html for single-page apps
    location / {
        try_files $uri $uri/ /index.html;
    }

    location = /50x.html {
        alias /var/www/html/build;
    }

    error_page  404     /404.html;
    error_page  403     /403.html;

    # To allow POST on static pages
    error_page  405     =200 $uri;

    ssl_certificate /Path/abc.crt;
    ssl_certificate_key /Path/abc.key;
}

explanation:

  1. The location /api/ block is designed to forward any API requests (e.g., /api/users) to your backend API server running on http://localhost:3000 (you can change this based on where your API is hosted).
  2. The location / block will continue serving static files (like index.html) for non-API requests, making it suitable for single-page applications (SPAs).
  3. Ensure that your API path correctly starts with /api/ or adjust the location block accordingly.If your API path is different (e.g., /v1/api or something else), you will need to adjust the location directive to match your API routes.

2 Comments

What should be the proxy_pass value if the backend is running in the same instance with pm2?
whatever it may be running it should be running in a port you just need to proxy pass the port, isnt your api running in 3000 as above??

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.