gpt4 book ai didi

node.js - 只需从本地主机 :80 to localhost:8080 not working 进行 nginx 反向代理

转载 作者:行者123 更新时间:2023-12-05 03:00:14 27 4
gpt4 key购买 nike

我第一次学习使用 nginx 的反向代理,以下内容对我不起作用我正在尝试重新路由来自 http://localhost 的请求到我在 http://localhost:8080 运行的 api 服务器

server {
listen 80;
location / {
proxy_pass http://localhost:8080;
}
}

当我点击 http://localhost ,我只是看到了欢迎使用 nginx 启动画面。如果我点击http://localhost:8080 , 我看到了我的 api

我有一个运行在 :8080 的 Node 快速服务,我可以手动点击它,但不应该 http://localhost也在那里代理?

最佳答案

当我设置一个将请求转发到 Node 服务器的 nginx 域时,它看起来像这样,对于 server_name,您可以使用 localhost 作为访问它的参数通过本地主机。您还可以传递 default_server 以使其成为默认服务器配置。

注意:只有一个事件配置可以包含default_server,否则Nginx会抛出错误。

注意:当使用default_server 时,Nginx 将在该服务器配置中捕获localhost。否则,您需要在 server_name 的列表中指定 localhost(以空格分隔)。

server {
# Setup the domain name(s)
server_name example.com;
listen 80 default_server;

# If you would like to gzip your stuff
gzip on;
gzip_min_length 1;
gzip_types *;

# Setup the proxy
# This will forward all requests to the server
# and then it will relay the servers response back to the client
location / {
proxy_pass http://127.0.0.1:8080;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_cache_bypass $http_upgrade;
}
}

关于node.js - 只需从本地主机 :80 to localhost:8080 not working 进行 nginx 反向代理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56954650/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com