Benjamin
Benjamin

Reputation: 3826

nginx reverse proxy is returning 404 error

I am trying to setup a reverse proxy that can redirect all requests from mysite.com:3005/Photos/ to mysite.com/Photos/

I have setup a reverse proxy inside the config file in sites-available folder.

Below is the config file setting:

 server {
  listen   80;
  root /var/www/html/mysite.com/site1/;
  index index.php index.html index.htm istaff.html index.cgi;
  server_name localhost *.mysite.com;

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

  location /site1/{
     proxy_pass http://127.0.0.1:80/site1/;
  }

  location /Photos/ {
      proxy_pass http://127.0.0.1:3005/Photos/;
  }

I can now access the image when I put :3005 in the URL, but without the port number, I am getting 404 error.

Upvotes: 0

Views: 5752

Answers (1)

brandonsimpkins
brandonsimpkins

Reputation: 574

Having logs and knowing what URL you are trying to access the images with would be helpful. It looks like the issue you are having is with the URL matching and forwarding.

Based on my read of your config file, accessing:

http://example.com/Photos/sample.jpg

Would proxy the http request to:

http://example.com:3005/Photos//Photos/sample.jpg

You're not getting a 404 error when you "put :3005 in the URL" because you're not actually using Nginx to proxy the call.

You should adjust your Nginx config to look like this:

server {
    listen   80;
    root /var/www/html/mysite.com/site1/;
    index index.php index.html index.htm istaff.html index.cgi;
    server_name localhost *.mysite.com;

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

    location /site1 {
        proxy_pass http://127.0.0.1:80;
    }

    location /Photos/ {
        proxy_pass http://127.0.0.1:3005;
    }
}

Depending on how Nginx is running you may have issues with the /site1 location forwarding traffic. It looks like it's routing traffic back to itself.But, it should work if Nginx is listening on the outer loop, and you're forwarding traffic to the inner loop.

Upvotes: 2

Related Questions