nginx proxy_pass POST 404 errors

5,978

My bad, the problem wasn't nginx, it was my app server. I was using a routing module that required me to explicitly specify the request method if not a get, and so that's why it was throwing a 404 error on post, but not when hitting the browser url directly.

Share:
5,978

Related videos on Youtube

Scott Klarenbach
Author by

Scott Klarenbach

Updated on September 18, 2022

Comments

  • Scott Klarenbach
    Scott Klarenbach over 1 year

    I have nginx proxying to an app server, with the following configuration:

    location /app/ {
            # send to app server without the /app qualifier
            rewrite /app/(.*)$ /$1 break;
            proxy_set_header Host $http_host;
            proxy_pass http://localhost:9001;
            proxy_redirect http://localhost:9001 http://localhost:9000;
        }
    

    Any request for /app goes to :9001, whereas the default site is hosted on :9000.

    GET requests work fine. But whenever I submit a POST request to /app/any/post/url it results in a 404 error. Hitting the url directly in the browser via GET /app/any/post/url hits the app server as expected.

    I found online other people with similar problems and added

    proxy_set_header Host $http_host;
    

    but this hasn't resolved my issue.

    There are no errors logged, and the access logs are as follows:

    # For http://localhost:9000 (no proxying)
    
    127.0.0.1 - - [08/Dec/2012:12:29:28 -0800] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.4 (KHTML, like Gecko) Chrome/22.0.1229.94 Safari/537.4"
    
    # For http://localhost:9000/app/get/priorities (GET proxy)
    
    127.0.0.1 - - [08/Dec/2012:12:32:17 -0800] "GET /app/get/priorities HTTP/1.1" 200 77 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.4 (KHTML, like Gecko) Chrome/22.0.1229.94 Safari/537.4"
    
    # When posting to http://localhost:9000/app/create/priority:
    
    127.0.0.1 - - [08/Dec/2012:12:33:50 -0800] "POST /app/create/priority/ HTTP/1.1" 404 188 "http://localhost:9000/form-test.html" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.4 (KHTML, like Gecko) Chrome/22.0.1229.94 Safari/537.4"
    

    Any insights are appreciated.

    Thanks.

    Full config below:

    server {
        listen   9000; ## listen for ipv4; this line is default and implied
        #listen   [::]:80 default_server ipv6only=on; ## listen for ipv6
    
        root /home/scott/src/ph-dox/html;
        # root ../html; TODO: how to do relative paths?
        index index.html index.htm;
    
        # Make site accessible from http://localhost/
        server_name localhost;
    
        location / {
            # First attempt to serve request as file, then
            # as directory, then fall back to displaying a 404.
            try_files $uri $uri/ /index.html;
            # Uncomment to enable naxsi on this location
            # include /etc/nginx/naxsi.rules
        }
    
        location /app/ {
            # rewrite here sends to app server without the /app qualifier
            rewrite /app/(.*)$ /$1 break;
            proxy_set_header Host $http_host;
            proxy_pass http://localhost:9001;
            proxy_redirect http://localhost:9001 http://localhost:9000;
        }
    
        location /doc/ {
            alias /usr/share/doc/;
            autoindex on;
            allow 127.0.0.1;
            allow ::1;
            deny all;
        }
    
    }
    
    • Fleshgrinder
      Fleshgrinder over 11 years
      Anything useful in your log files? Btw. relative paths for any directive within nginx configurations are relative to the installation path of nginx, in 99% this is /etc/nginx.
    • Scott Klarenbach
      Scott Klarenbach over 11 years
      I'll append the access.log details to the question.
  • Fleshgrinder
    Fleshgrinder over 11 years
    Glad you found the problem.