nginx设置重定向跳转后ip:[端口]/abc变成ip/abc而报错404

这篇具有很好参考价值的文章主要介绍了nginx设置重定向跳转后ip:[端口]/abc变成ip/abc而报错404。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

nginx设置重定向跳转后  ip:[端口]/abc  变成  ip/abc  而报错404

nginx配置:

    server {
        listen    80;
        server_name  _;
        client_max_body_size 300m;
        absolute_redirect off;


        location / {
            root     html;
            index  index.html index.htm;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Host $host;
        }

    ## abc目录
        location /abc/ {
        if (-d $request_filename) {
           rewrite [^/]$ $scheme://$http_host$uri/ permanent;
        }
            proxy_pass http://localhost:8088/; 
            ##由于规定了流量出口必须是80,所以得在80的server上代理出8088端口的abc项目
            proxy_set_header Host $host:$server_port;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header REMOTE-HOST $remote_addr;
            proxy_http_version 1.1;
        proxy_read_timeout   3600s;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
            add_header X-Cache $upstream_cache_status;
        add_header Cache-Control no-cache;

        }

 

server {
        listen       8088;
        server_name  _;
        add_header Access-Control-Allow-Origin *;
        add_header Access-Control-Allow-Headers X-Requested-With;
        add_header Access-Control-Allow-Methods GET,POST,OPTIONS;
        charset utf-8;
        location / {
            root /opt/abc/;
            index  index.html index.htm;
            autoindex_exact_size off;
            autoindex_localtime on;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Host $host:$server_port;

        }
        }

本地测试项目是否能正常访问:

http://192.168.1.22:8088 正常访问

http://192.168.1.22/abc 正常访问

当我需要将192.168.1.22:8088映射到外网ip:[port] 进行外网访问时报错。

假设外网ip=122.23.43.21

路由分配:122.23.43.21:5566  指向内网  --> 192.168.1.22:8088

也就是说,当我访问 122.23.43.21:5566/abc 时 可访问http://192.168.1.22:8088 项目

但是当我使用外网地址访问项目时:

http://122.23.43.21:5566/abc/    项目正常访问

http://122.23.43.21:5566/abc 项目由 

http://122.23.43.21:5566/abc   重定向为 http://122.23.43.21/abc/   而导致报错404

也就是说在nginx.conf里,nginx把所有的ip:[port]都会自动跳转成ip:80 也就是默认重定向后是80端口,而外网ip的80端口是没有作映射的,所以404

此时我们需要知道,重定向错误,必定出在nginx上。所以需要从server里下手

在反向代理中添加 absolute_redirect off; 

 server {
        listen       8088;
        server_name  _;
        add_header Access-Control-Allow-Origin *;
        add_header Access-Control-Allow-Headers X-Requested-With;
        add_header Access-Control-Allow-Methods GET,POST,OPTIONS;
        charset utf-8;
        location / {
            root /home/web/zhuhai-forest-resources-management/;
        index  index.html index.htm;
            autoindex off; ##防止ip:[port]/test 重定向请求后变成 ip/test而导致404
            autoindex_exact_size off;
            autoindex_localtime on;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Host $host:$server_port;

        }
        }

即可成功访问

参考如下文章:

nginx重定向导致端口消失无法正常访问

场景

访问路径带端口http://ip:端口/login,回车后端口消失http://ip/login无法正常访问

解决方法

nginx配置中中加一句:

新版本nginx(≥1.11.8)

absolute_redirect off;

旧版本nginx(<1.11.8)

port_in_redirect off;
nginx设置重定向跳转后ip:[端口]/abc变成ip/abc而报错404

番外:

1. 关于nginx自动跳转

站点目录结构:

根目录:/var/wwwroot/test/

nginx设置重定向跳转后ip:[端口]/abc变成ip/abc而报错404

当访问地址为 http://www.test.com/abc 时,因为nginx未找到abc这个文件,而是找到了abc目录,自动在地址后面加上了斜杠,所以自动发生 301 跳转到 http://www.test.com/abc/

2. nginx自动跳转相关的3个配置

Nginx中存在一三个跳转的相关配置:

1. absolute_redirect

Syntax:	absolute_redirect on | off;Default:	absolute_redirect on;Context:	http, server, locationThis directive appeared in version 1.11.8.If disabled, redirects issued by nginx will be relative.See also server_name_in_redirect and port_in_redirect directives.

默认为开启。

2. server_name_in_redirect

Syntax:	server_name_in_redirect on | off;Default:	server_name_in_redirect off;Context:	http, server, locationEnables or disables the useof the primary server name, specified by the server_name directive, in absolute redirects issued by nginx. When the useof the primary server name is disabled, the name from the “Host” request header field is used. If this field isnotpresent, the IP addressof the server is used.The use of a port in redirects is controlled by the port_in_redirect directive.

默认关闭

3. port_in_redirect

Syntax:	port_in_redirect on | off;Default:	port_in_redirect on;Context:	http, server, locationEnables or disables specifying the port in absolute redirects issued by nginx.The use of the primary server name in redirects is controlled by the server_name_in_redirect directive.

默认开启。

3. 举例

访问地址为:http://192.168.1.232/abc

Nginx情况一:

server {    listen 80;    server_name test.com;    absolute_redirect off; // 关闭了它,不管 server_name_in_redirect, port_in_redirect 怎么设置都没有用    server_name_in_redirect on;    port_in_redirect on;}

发生 301 跳转,跳转后的地址为:http://192.168.1.232/abc/

官方说是使用 请求头中的 Host 字段,作为url跳转后host部分

nginx设置重定向跳转后ip:[端口]/abc变成ip/abc而报错404

Nginx情况二:

server {    listen 80;    server_name test.com;    absolute_redirect on; // 默认为on, Nginx配置中没有这个字段其实值是on, server_name_in_redirect, port_in_redirect 会根据配置起作用    server_name_in_redirect on; // 发生跳转时使用 server 中配置的域名    port_in_redirect on; // 发生跳转时使用 server 中配置的端口}

发生 301 跳转,跳转后的地址为:http://test.com/abc/文章来源地址https://www.toymoban.com/news/detail-474148.html

到了这里,关于nginx设置重定向跳转后ip:[端口]/abc变成ip/abc而报错404的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • nginx :80跳转其他端口

    以docker nginx为例 修改docker-env.yml文件增加端口映射 修改nginx配置文件,增加server nginx -t  #测试配置文件是否通过 重新build和重启nginx 服务 测试 http://xxx.xxx.com 是否跳转到对应的端口(如8001)

    2024年02月14日
    浏览(33)
  • vue路由跳转后,刷新指定页面。

            做项目遇到一个坑:A页面带参跳转到B页面,第二次跳转时的参数与第一次时的参数不同,但是后台查询时还是使用的第一次的参数。需要手动刷新之后,才会使用第二次参数。         鉴于时间原因直接使用刷新页面监听路由的方法。在准备跳转的A页面添加路

    2024年02月12日
    浏览(57)
  • 实战指南:使用 Nginx 反向代理实现多端口跳转

    在现代 Web 开发中,Nginx作为一款高性能的开源反向代理服务器,提供了强大的功能来管理网络流量和路由。本文将介绍如何利用 Nginx 的反向代理功能,将不同路径的请求转发到不同端口的 Tomcat 服务上,以实现多端口跳转的效果。 使用 Nginx 的强大反向代理功能,我们能够根

    2024年02月19日
    浏览(41)
  • 微信小程序优化多次跳转后卡顿问题

    一、微信小程序多次跳转会产生卡顿的原理 通过wx.navigateTo 跳转,都会出现保留当前页面,打开新的页面机制。 wx.navigateTo不会将旧页面出栈,会将新页面入栈(栈内元素个数增加,栈内元素5个时,不能再跳转)。手机性能好点,可能10次左右才会导致小程序跳转卡顿崩溃。 二

    2024年02月11日
    浏览(92)
  • Nginx配置HTTPS跳转到非443端口的技巧和注意事项

    近一段时间由于看到v*云服务厂商有活动,就注册并开了台云服务器,试一下区别。 (“充10美元送30天内有效的250美元的免费额度,意思是30天内在 你加起来 不超出250美元的 服务随便开,但是注意的是30天后这就不免费了,记得及时关闭。只支持paypal,而阿里alipay一般是充值

    2023年04月18日
    浏览(40)
  • vue单页面实现路由跳转后保留原页面数据

    有时候在路由跳转后,返回原页面时需要保留之前的数据,即不销毁页面。 页面的缓存,需要用到vue的内置组件keep-alive,来缓存列表页面,同时配合路由选项来更改页面的数据。 在设置keep-alive缓存的组件中,首次进入组件,会一次调用组件的钩子函数:created -- mounted --ac

    2024年02月15日
    浏览(33)
  • 关于vue路由跳转后的页面不会刷新的解决办法

    做uni项目的时候遇到的情况:1,父组件页面刷新了之后 需要触底加载的子组件能触底加载,否则不能加载。2,子组件不能触底加载,只需要刷新之后就能触底加载。以上的刷新不包括下拉刷新。3. 可能因为包了一层tab页导致跳转进页面没有请求数据的问题。 我的解决办法是

    2024年02月11日
    浏览(51)
  • nginx 映射ip端口服务

    笔者只对外开放了一个ip端口,但实际使用不止一个端口,那么需要把ip端口映射出来。这里使用Nginx来映射ip端口。比如将前端系统的ip端口映射出去了,还需要后台的ip端口,这样前后端才能结合使用 通过后台服务名(dwiot-cloud)来分发到后台服务ip端口,这样可以直接通过

    2024年02月11日
    浏览(25)
  • Selenium页面跳转后的元素定位-switch_to.window()使用

    在使用Selenium获取网页数据时常常会因为页面跳转导致,后续的页面数据无法正常获取。究极原因,都是因为此时的页面对象还是上一个页面,使用当前页面的定位条件当然会报错(因为上一个页面根本不存在这些定位条件)。因此我们可以通过switch_to.window()进行页面的切换

    2023年04月09日
    浏览(29)
  • nginx安装及(域名、端口、ip)配置

    1、首先安装pcre库 说明:pcre使nginx具备URL重写的rewrite模块 1)查看系统环境 cat /etc/redhat-release 2)显示64位系统 uname -r  、 uname -m  2、yum安装pcre命令 配置下载源 (1)安装:yum install -y pcre pcre-devel 安装后查看:rpm -qa pcre pcre-devel 注意:nginx安装依赖pcre、pcre-devel 、openssl、open

    2024年02月05日
    浏览(33)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包