简体   繁体   English

nodejs 应用程序工作,但 nginx 代理没有捕捉到它

[英]nodejs application work but nginx proxy doesn't catch it

I have got a Nodejs application running on my server so I wanted to use Nginx reverse proxy to have the API calls from a nice domain name instead of typing IP and port and I also generated a free let's encrypt certificate to use for that.我有一个 Nodejs 应用程序在我的服务器上运行,所以我想使用 Nginx 反向代理从一个不错的域名调用 API,而不是输入 IP 和端口,我还生成了一个免费的 Let's encrypt 证书来使用。

the problem is the reverse server doesn't work if I use an IP and port then it works if I use domain name it gives me an Nginx template test page anyways here are my configs :问题是,如果我使用 IP 和端口,反向服务器不起作用,然后如果我使用域名,它就会工作,它会给我一个 Nginx 模板测试页面,无论如何这里是我的配置:

Nginx config http { log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; Nginx 配置 http { log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"';

access_log  /var/log/nginx/access.log  main;

sendfile            on;
tcp_nopush          on;
tcp_nodelay         on;
keepalive_timeout   65;
types_hash_max_size 2048;

include             /etc/nginx/mime.types;
#include /etc/nginx/sites-enabled/*;
default_type        application/octet-stream;

# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf;

server {
    listen       80 default_server;
    listen       [::]:80 default_server;
    server_name  _;
    root         /usr/share/nginx/html;

    # Load configuration files for the default server block.
    include /etc/nginx/default.d/*.conf;

    location / {
    }

    error_page 404 /404.html;
        location = /40x.html {
    }

    error_page 500 502 503 504 /50x.html;
        location = /50x.html {
    }
}

my reverse proxy config and location我的反向代理配置和位置

/etc/nginx/conf.d/api1.conf /etc/nginx/conf.d/api1.conf

Backend API server { server_name api.domain.com;后端 API 服务器 { server_name api.domain.com;

location / {
    proxy_pass http://localhost:26423/all; #whatever port your app runs on
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection 'upgrade';
    proxy_set_header Host $host;
    proxy_cache_bypass $http_upgrade;
}

} }

enter image description here在此处输入图片说明

whenever I try to see the API from a browser or postman I get this page每当我尝试从浏览器或邮递员那里查看 API 时,我都会看到此页面

It's not clear if you have a domain or not.目前尚不清楚您是否拥有域。 If you have one, it should point to the server's public ipv4 ip and then create a nginx vhost that proxies to your nodejs app.如果你有一个,它应该指向服务器的公共 ipv4 ip,然后创建一个代理到你的 nodejs 应用程序的 nginx 虚拟主机。

Assuming your public server ipv4 ip address is 1.2.3.4 and you have a domain like nodejsappdomain.com, then the nodejsappdomain.com should point to 1.2.3.4 and the nginx vhost will be something like this:假设你的公共服务器 ipv4 ip 地址是 1.2.3.4 并且你有一个像 nodejsappdomain.com 这样的域,那么 nodejsappdomain.com 应该指向 1.2.3.4 并且 nginx vhost 将是这样的:

server { listen 80;服务器{听80;

server_name nodejsappdomain.com www.nodejsappdomain.com;

location / {
    proxy_pass  http://127.0.0.1:nodejsapp_port;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
}

} }

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

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