Problem:Nginx不根据我在单独配置文件中定义的规则路由流量,只显示默认的404响应。
Context:我有一个用Go编写的小型中间件应用程序,它为GET请求提供了一个简单的响应。应用程序部署在端口8080上:
$ curl localhost:8080
ok我希望编写一个Nginx配置,它允许我将呼叫从/api路由到/api,这将允许我执行以下操作
$ curl localhost/api
ok为此,我编写了以下配置:
/etc/nginx/sites-available/custom-nginx-rules
server {
listen 80;
location /api {
proxy_pass http://localhost:8080;
}
}我还在/etc/nginx/sites-enabled/中为上面的文件创建了一个软链接
$ ls -l /etc/nginx/sites-enabled
total 0
lrwxrwxrwx 1 root root 34 Jan 19 16:42 default -> /etc/nginx/sites-available/default
lrwxrwxrwx 1 root root 32 Feb 20 14:56 custom-nginx-rules -> /etc/nginx/sites-available/custom-nginx-rules其余的设置是香草Nginx,什么都没有改变。尽管设置很简单,但在进行以下调用时,我将得到一个404:
$ curl localhost/api
<html>
<head><title>404 Not Found</title></head>
<body bgcolor="white">
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.10.3</center>
</body>
</html>其他信息:下面是安装在我的系统上的nginx包(运行在raspberry pi上)
$ dpkg -l | grep nginx
ii libnginx-mod-http-auth-pam 1.10.3-1+deb9u1 armhf PAM authentication module for Nginx
ii libnginx-mod-http-dav-ext 1.10.3-1+deb9u1 armhf WebDAV missing commands support for Nginx
ii libnginx-mod-http-echo 1.10.3-1+deb9u1 armhf Bring echo and more shell style goodies to Nginx
ii libnginx-mod-http-geoip 1.10.3-1+deb9u1 armhf GeoIP HTTP module for Nginx
ii libnginx-mod-http-image-filter 1.10.3-1+deb9u1 armhf HTTP image filter module for Nginx
ii libnginx-mod-http-subs-filter 1.10.3-1+deb9u1 armhf Substitution filter module for Nginx
ii libnginx-mod-http-upstream-fair 1.10.3-1+deb9u1 armhf Nginx Upstream Fair Proxy Load Balancer
ii libnginx-mod-http-xslt-filter 1.10.3-1+deb9u1 armhf XSLT Transformation module for Nginx
ii libnginx-mod-mail 1.10.3-1+deb9u1 armhf Mail module for Nginx
ii libnginx-mod-stream 1.10.3-1+deb9u1 armhf Stream module for Nginx
ii nginx 1.10.3-1+deb9u1 all small, powerful, scalable web/proxy server
ii nginx-common 1.10.3-1+deb9u1 all small, powerful, scalable web/proxy server - common files
ii nginx-full 1.10.3-1+deb9u1 armhf nginx web/proxy server (standard version)我还要求此设置独立于任何主机或服务器名称。不管主机是什么,它都应该执行路由。
发布于 2018-02-21 17:37:35
在没有看到完整配置的情况下,默认的nginx server块似乎是接受请求而不是您的请求。您可以通过将侦听行更改为:
`listen 80 default_server;` 您还可以通过添加一个server_name并使用它来确定是否是这样的:
server_name api.example.com;
然后:curl -H "Host: api.example.com" http://localhost/api
如果这样做有效,那么问题肯定是default_server处理。
来自关于服务器选择的NGINX文档
在此配置中,nginx只测试请求的头字段“Host”,以确定请求应该路由到哪个服务器。如果其值与任何服务器名称不匹配,或者请求根本不包含此标头字段,则nginx将将请求路由到此端口的默认服务器。在上面的配置中,默认服务器是第一个--这是nginx的标准默认行为。还可以显式地设置默认服务器,并在listen指令中使用default_server参数:
https://stackoverflow.com/questions/48887517
复制相似问题