I want to transfer all post requests to port 5000 as that's where my express server is running. This is my nginx config at the moment:
server {
if ($host = www.website.com) {
return 301 https://$host$request_uri;
} # managed by Certbot
listen 80 default_server;
server_name www.website.com;
return 301 https://$server_name$request_uri;
}
server {
listen 443 ssl;
server_name www.website.com;
ssl_certificate /etc/letsencrypt/live/website.com/fullchain.pem; # managed$
ssl_certificate_key /etc/letsencrypt/live/website.com/privkey.pem; # manag$
I know that I should have probably thought about it before writing the code and make all post requests to /api and then redirect it from the config. But I haven't, and I don't want to change the code if that's node necessary.
How can I recognize if a request is a post request and transfer it to port 5000?
Also, in what language is this config file written in? it looks like js but it isn't really
You'd better change your code, but as a quick and dirty hack you can try
server {
...
if ($request_method = POST) {
rewrite ^ /api$request_uri last;
}
location / {
# your default location config here
}
location /api/ {
proxy_pass http://127.0.0.1:5000/;
}
}
NGINX config is not a programming language, it uses its own syntax and is rather declarative than imperative, changing the order of nginx directives (except of those from ngx_http_rewrite_module
) usually make no difference.