1

I'm writing a REST API (nodejs/express) that's running locally on my server (listening to port 3000); I have an existing Apache server set up, and I want to configure it to reroute api calls to the node instance. Below is my current Apache config, using ProxyPass:

<VirtualHost _default_:80>
  DocumentRoot "/opt/bitnami/apache2/htdocs"
  <Directory "/opt/bitnami/apache2/htdocs">
    Options Indexes FollowSymLinks
    AllowOverride All
    <IfVersion < 2.3 >
      Order allow,deny
      Allow from all
    </IfVersion>
    <IfVersion >= 2.3 >
      Require all granted
    </IfVersion>
  </Directory>

  # Error Documents
  ErrorDocument 503 /503.html

  # Bitnami applications installed with a prefix URL (default)
  Include "/opt/bitnami/apache2/conf/bitnami/bitnami-apps-prefix.conf"
  ProxyPreserveHost On
  ProxyPass /api http://localhost:3000
  ProxyPassReverse /api http://localhost:3000
</VirtualHost>

The issue I'm having is that PUT and POST requests are getting converted to GET requests when they get passed from Apache to Node; I didn't originally think it was this issue since I'm not sending a redirect code to the client at all, but I don't have another better guess. Has anyone seen this issue before // have a way to force the requests to maintain their type when they get passed through?

1 Answers1

2

I just had this problem happen to me with a POST getting converted into a GET!

The answer comes too late for you but it may help somebody else like me, stuck in this issue.

Here is the relevant piece of the script in my file

<Location /redirect_directory/> ProxyPass http://127.0.0.1:NODE_APP_PORT/ ProxyPassReverse http://127.0.0.1:NODE_APP_PORT/

Drove me nuts! Tried several solutions mentioned in stackoverflow about using

#       Handle node REST API
#        RewriteCond %{REQUEST_URI} /redirect_directory/ [NC]
#        RewriteRule /redirect_directory/(.*) http://127.0.0.1:NODE_APP_PORT/$1?%{QUERY_STRING} [R=307] [P,L]
#       proxyPassReverse /noderestshop/ http://127.0.0.1:NODE_APP_PORT

where you use the 307 redirect code instead of the default 301. But nothing worked!

What was the problem?

Well, I was using POSTMAN! and Postman version 8 was sending my post requests using http without explicitly stating it in the url line. My config file (.cof) in my /etc/apache2/sites-enabled/ for the corresponding virtual host, was converting it to an https request using:

#       Handdle redirection of http to https
        RewriteCond %{SERVER_NAME} =spinelli.io [OR]
        RewriteCond %{SERVER_NAME} =www.spinelli.io
        RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]

And THAT conversion to https is what was mutating the POST requests into GET. Something that makes sense from a security point of view! You just don't pass non-secure POST requests into your https!

I changed the request in the postman url line to https:// and ALBRICIAS! it worked!

Julio Spinelli
  • 587
  • 3
  • 16