[英]Simply nginx reverse proxy from localhost:80 to localhost:8080 not working
im learning reverse proxy w/ nginx for the first time, and the following isnt working for me im trying to reroute requests from http://localhost to an api server i have running at http://localhost:8080 我第一次学习带有nginx的反向代理,以下内容对我不起作用,我试图将请求从http:// localhost重新路由到我在http:// localhost:8080运行的api服务器
server {
listen 80;
location / {
proxy_pass http://localhost:8080;
}
}
when i hit http://localhost , I simply get shown the welcome to nginx splash screen. 当我点击http:// localhost时 ,我只是收到欢迎使用Nginx启动画面的提示。 if i hit http://localhost:8080 , i see my api
如果我点击http:// localhost:8080 ,我会看到我的api
I have a node express service running at :8080, which i can hit manually, but shouldn't http://localhost be proxied there too? 我有一个运行在:8080的节点快递服务,我可以手动打它,但是http:// localhost也不应该被代理吗?
When I setup a nginx domain that forwards requests to a node server, it looks like this, for the server_name
, you can use localhost
as a parameter for accessing it via localhost. 当我设置将请求转发到节点服务器的nginx域时,对于
server_name
,您可以将localhost
用作通过localhost访问它的参数。 You can also pass default_server
to make this the default server config. 您还可以传递
default_server
使其成为默认服务器配置。
Note: Only one active config can contain default_server
otherwise Nginx will throw errors. 注意:只有一个活动配置可以包含
default_server
否则Nginx会抛出错误。
Note: When using default_server
, Nginx will catch localhost
in that server config. 注意:使用
default_server
,Nginx将在该服务器配置中捕获localhost
。 Otherwise you need to specify localhost
in the list of server_name
's (separated by a space). 否则,您需要在
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;
}
}
Found out that adding this to my nginx.conf fixes the issue: 发现将其添加到我的nginx.conf中可以解决此问题:
listen [::]:80;
For some reason listen 80;
出于某种原因,
listen 80;
doesn't catch my http://localhost requests. 无法捕获我的http:// localhost请求。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.