nginx响应"301永久移动" [英] nginx responding "301 moved permanently"

查看:541
本文介绍了nginx响应"301永久移动"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请考虑以下nginx配置文件:

Consider the following nginx config file:

server {
    listen 443;
    ssl    on;
    ssl_certificate     /etc/tls/cert.pem;
    ssl_certificate_key /etc/tls/key.pem;
    location / {
        proxy_pass http://api.default.svc.cluster.local;
    }
}

在443上的所有传入TCP请求都应重定向到在api.default.svc.cluster.local:80(这是一个REST-Api btw节点)上运行的服务器.效果很好,我可以按预期curl https://<nginx-IP>/ nginx并获得正确的响应.

All incoming TCP requests on 443 should redirect to my server running on api.default.svc.cluster.local:80 (which is a node REST-Api btw). This works fine, I can curl https://<nginx-IP>/ nginx and get a correct response, as expected.

现在,我想将位置从/更改为/api,所以我可以触发curl https://<nginx-IP>/api以获得与以前相同的响应.

Now, I'd like to change the location from / to /api, so I can fire a curl https://<nginx-IP>/api in order to get the same response as before.

1.尝试

所以我将配置中的位置线更改为:

So I change the location line in the config to:

location /api {

不幸的是,这是行不通的,相反,我得到一个错误Cannot GET /api,这是一个节点错误,因此很显然它被路由到了api,但是仍然有些臭味.

Unfortunately this won't work, instead I get an error Cannot GET /api which is a node error, so obviously it gets routed to the api but something's still smelly.

2.尝试

似乎URI中必须包含斜杠,因此我将其添加到了该位置:

It seems as the trailing slash in an URI is required so I added it to the location:

location /api/ {

现在,情况有所改变.我不会得到与以前相同的错误,而是得到"301永久移动".如何修复我的Nginx配置文件?

Now something changed. I won't get the same error as before, instead I get an "301 moved permanently". How can I fix my nginx config file?

有关环境的其他信息

我正在使用kubernetes部署,该部署部署了nginx反向代理(包括).配置介绍.然后,我使用kubernetes服务公开nginx.另外,我尝试使用kubernetes入口使用相同的路由来处理这种情况,但是入口服务将以default backend - 404消息作为响应.

I'm using a kubernetes deployment that deploys the nginx reverse proxy incl. the config introduced. I then expose nginx using a kubernetes service. Also, I tried using kubernetes ingress to deal with this situation, using the same routes, however, the ingress service would respond with a default backend - 404 message.

推荐答案

如问题中所述,URI中的斜杠很重要.我在位置修复了该问题,但是没有将它添加到使用proxy_pass传递的URI中.

As mentioned in the question, trailing slashes in URIs are important. I fixed this in the location, however, I didn't add it to the URI I pass using proxy_pass.

对于nginx代理,我使用以下配置使其工作:

As for the nginx proxy I got it to work using the following config:

server {
    listen 443;
    ssl    on;
    ssl_certificate     /etc/tls/cert.pem;
    ssl_certificate_key /etc/tls/key.pem;
    location /api/ {
        proxy_pass http://api.default.svc.cluster.local/;
    }
}

关于入口解决方案,我无法通过在路径中添加缺少的尾部斜杠来使其正常工作.由于服务的名称而指定了该服务,因此不能添加斜杠(即会导致错误).

Concerning the ingress solution, I was not able to get it to work by adding the missing trailing slash to the path. The service is specified due its name and therefore no trailing slash can be added (i.e. it would result in an error).

这篇关于nginx响应"301永久移动"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆