简体   繁体   English

如何使用 nginx 将 post 请求传输到不同的端口?

[英]How do I transfer post requests to a different port with nginx?

I want to transfer all post requests to port 5000 as that's where my express server is running.我想将所有发布请求转移到端口 5000,因为那是我的快速服务器运行的地方。 This is my nginx config at the moment:这是我目前的 nginx 配置:

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.我知道在编写代码并将所有发布请求发送到 /api 之前我应该考虑一下,然后从配置中重定向它。 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?如何识别请求是否为 post 请求并将其传输到端口 5000?

Also, in what language is this config file written in?另外,这个配置文件是用什么语言写的? it looks like js but it isn't really看起来像 js 但实际上不是

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. NGINX 配置不是一种编程语言,它使用自己的语法并且是声明性的而不是命令性的,改变 nginx 指令的顺序(除了来自ngx_http_rewrite_module的指令)通常没有区别。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM