1

How do I return the HTTP STATUS CODE from the http://127.0.0.1:8080/

Example:

and so on for the any other status code, please help??? spent a few hours with this one...

pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;

    gzip  on;
    server {
        listen       80;
        server_name  127.0.0.1;

        location / {
            proxy_pass http://127.0.0.1:8888;
            proxy_http_version  1.1;
            proxy_pass_request_headers on;
            gzip_static on;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_redirect off;
            proxy_set_header        Host            $host;
            proxy_set_header        Status          $status;
        }

        location /api/ {
            proxy_pass http://127.0.0.1:8080;
            proxy_http_version  1.1;
            proxy_pass_request_headers on;
            gzip_static on;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_redirect off;
            proxy_set_header        Host            $host;
            proxy_set_header        Status          $status;
        }
    }

}
  • Your request to `:8080` doesn't appear to be going through nginx, it sounds like you're hitting the upstream server directly, as you'd need to be hitting port 80 for nginx to handle the request (assuming your browser/client is running on the same machine) What response do you get when hitting `http://127.0.0.1/api/example`. The wording sounds like you want to see a 500 here, but I can't understand why? – v25 Jan 17 '19 at 15:21
  • @v25 I would like to pass various STATUS_CODE's to the browser when a request hits the api call i return a json object, and if for example, i want to say that the request requires an auth token provided via cookies, i would then send a json object with a message and a 403, and so on... – Dean Van Greunen Jan 18 '19 at 18:07
  • nginx already does this, unless you instruct it to not do so. But it doesn't appear that you have done that. Exactly what is the problem you are having? – Michael Hampton Jan 18 '19 at 18:43

1 Answers1

1

It seems you have 3 host:port combinations at play here:

  • 127.0.0.1:80 or simply 127.0.0.1 - which nginx is serving
  • 127.0.0.1:8888 - first app server with the endpoint /
  • 127.0.0.1:8080 - second app server with the endpoint /api/

http://127.0.0.1:8080/api/example returns a 500 then when accessing ttp://127.0.0.1/api/example it should also return 500,

I'm not really sure what you mean here.

If you're hitting the former URL then you're connecting directly to the second app server, and recieving a 500 response.

If you're hitting the latter URL then you're connecting to nginx, which proxies the request to the second app server and should return the same response.

pass various STATUS_CODE's to the browser when a request hits the api call i return a json object, and if for example, i want to say that the request requires an auth token provided via cookies, i would then send a json object with a message and a 403, and so on...

This sounds like functionality which should be implemented on the app server (Wether it's Flask, Node.JS, or whatever) and not at an nginx level. nginx does have the ability to return status codes but this would be done in static location blocks (for example to return 404 in the case that a static file isn't found).

If your app server returns a status code then this should be passed back to the browser. Unless you're saying that's what's not happening.

Perhaps edit the location blocks slightly. Here's what I'm using and which also works with cookies. Note the trailing slash at the end of the proxy_pass line as this has caught me out in the past:

location  / {
    proxy_set_header Host $http_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://localhost:8888/;
}
v25
  • 748
  • 1
  • 6
  • 13