AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / server / 问题

问题[redirect](server)

Martin Hope
CDuv
Asked: 2022-10-12 02:18:27 +0800 CST

将所有 URL 重定向到除一个路径之外的其他(外部)站点?

  • 0

我有一个工作网站(http://www.example.com)(使用 PHP-FPM),我想将所有传入流量重定向到另一个网站(ttps://www.other-example.com/foo.html)上的 URL,但一个特定路径()除外/api。

这是我尝试过的 Nginx 配置(将 PHP 处理的东西放在专用location的 for/api和return 301on 中location /):

upstream php {
    server unix:/var/run/php/php7.4-fpm.sock;
}

server {
    listen 80;
    listen [::]:80;

    server_name www.example.com;

    root /var/www/www.example.com;
    index index.php;

    location  ~ ^/api(?:/(.*))?$ {
        #return 418;
        try_files $uri $uri/ /index.php$is_args$args;
    
        location ~ \.php$ {
            #include snippets/fastcgi-php.conf;

            # regex to split $uri to $fastcgi_script_name and $fastcgi_path
            fastcgi_split_path_info ^(.+?\.php)(/.*)$;

            # Check that the PHP script exists before passing it
            try_files $fastcgi_script_name =404;

            # Bypass the fact that try_files resets $fastcgi_path_info
            # see: http://trac.nginx.org/nginx/ticket/321
            set $path_info $fastcgi_path_info;
            fastcgi_param PATH_INFO $path_info;

            fastcgi_index index.php;
            include fastcgi.conf;
            include fastcgi_params;

            fastcgi_pass php;
        }
    }

    location / {
        return 301 https://www.other-example.com/foo.html;
    }
}

但是我所有的请求(http://www.example.com和http://www.example.com/foo)http://www.example.com/api都会得到一个HTTP/1.1 301 Moved Permanentlywith Location: https://www.other-example.com/foo.html。

我知道这些location ~ ^/api(?:/(.*))?$作品,因为如果我取消注释,return 418;我会收到 418http://www.example.com/api个请求响应,但其他请求会收到 301 个响应。

redirect nginx
  • 1 个回答
  • 18 Views
Martin Hope
Totor
Asked: 2022-03-29 06:39:40 +0800 CST

即使使用竞争位置正则表达式也使 nginx 重定向到 HTTPS

  • 0

server {...}我在 nginx块中有以下配置:

location /someapp { 
  if ( $https != "on" ) { 
    return 301 https://$server_name$request_uri;
  } 

  location ~ \.php$ { 
    include snippets/fastcgi-php.conf;
    fastcgi_pass unix:/var/run/php/php-fpm.sock;
  } 
} 

问题是:

  • 当我访问http://example.com/someapp/somefile.html(或只是/someapp)时,我被重定向到 HTTPS,
  • 但是当我访问时http://example.com/someapp/somefile.php,我没有被重定向到 HTTPS。

顺便说一句,这与doc一致,即:

为了找到与给定请求匹配的位置,nginx 首先检查使用前缀字符串(前缀位置)定义的位置。其中,匹配前缀最长的位置被选中并记忆。然后按照它们在配置文件中出现的顺序检查正则表达式。正则表达式的搜索在第一次匹配时终止,并使用相应的配置。如果找不到与正则表达式的匹配项,则使用前面记住的前缀位置的配置。

所以 when location ~ \.php$is a match,location /someapp被忽略,即使请求是 for .../someapp/somefile.php。

将location ~ \.php$ {...}块放在父location /someapp {...}块之外不会改变这种行为。

如何将每个 HTTP 重定向到 HTTPS 请求,/someapp 而不if必将and行复制return到 php 位置块中?

redirect nginx
  • 1 个回答
  • 238 Views
Martin Hope
hrthstnd
Asked: 2022-03-08 06:14:17 +0800 CST

将子域重定向到不同的 IP

  • 0

此问题最初发布在 Network Engineering StackExchange 页面上,但已移至此处。

TL;DR我有一个附加到 AWS 弹性 IP 的域名,我想为重定向到完全不同 IP 的子域创建记录。

您好,目前我正在将网站作为个人项目。我的设置当前是一个运行 RHEL 的 AWS EC2 实例,具有与之关联的弹性 IP。目前,我只安装了带有概念验证 index.html 的 apache HTTPd(最终计划切换到 Tomcat)。

我还通过 AWS Route 53 注册了一个域名,并成功为主网站创建了一条记录。即,导航到我的域正确显示我的网站。现在,与 AWS 服务无关,我在自己的网络上托管了一个 PLEX 服务器(PLEX 是媒体服务器)。您可以通过导航到我自己的个人 IP 地址直接访问此 PLEX 服务器(不推荐,但目前用于测试目的)。我修改了路由器的防火墙,将所有传入数据从端口 80 重定向到我拥有的特定 PLEX 端口。换句话说,通过直接在浏览器中导航到我的 IP,PLEX 服务器会加载。

当我尝试为子域创建记录时(在我的示例中,子域将是 watch.mydomain.name,其中 watch 是子域),页面永远不会加载(连接超时)。我认为这不是 PLEX 服务器的问题,而是我的 EC2 如何处理重定向。我很好奇 SSL 是否也在起作用,因为在尝试导航到子域时,它会自动尝试 https。请注意,主网站确实有证书,尽管它是自签名的。这意味着主域可以用 https 连接。我尝试创建一个 A 记录,类似于我的 EC2 IP 的记录,但使用的是我的个人 IP。我尝试了各种端口,但重定向 80 -> plex 端口似乎最有意义。我对解决方案一无所知,并且不确定这些可能的错误将记录在谁/哪里。

感谢您提供任何建议或解决方案。如果您需要更多信息,请告诉我。

domain-name-system redirect amazon-web-services
  • 1 个回答
  • 384 Views
Martin Hope
pok4
Asked: 2022-03-02 12:58:18 +0800 CST

将规则从 apache 重写为 nginx

  • 0

我对此有疑问...我正在使用免费托管为我自己的 cms 的未来买家提供演示。

主机是hubuhost.com,他们支持最新版本的php 8.1。问题是它们在 nginx 上并且不支持我的 apache 的 htaccess 规则。

我的配置是:

<IfModule mod_negotiation.c>
#Important rules for our system
Options -Multiviews -Indexes +FollowSymLinks
</IfModule>

#app.php instead of index.php
<IfModule mod_dir.c>
DirectoryIndex core.php index.php index.html
</IfModule>

#If 404 - redirect to 404 page
ErrorDocument 404 /404/index.php

<IfModule mod_rewrite.c>
#ModRewrite ON
RewriteEngine on 

#News SEO Urls
RewriteRule  ^topic_(.+?)$ core.php?id=$1

#Router
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ core.php [QSA,L]
 
#remove end trailing slash from urls
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /(.*)/$
RewriteRule ^ /%1 [R=301,L]
</IfModule>

#Block ENV access
<IfModule mod_version.c>
<Files "config.env">
   Order allow,deny
    Deny from all
</Files>
</IfModule>

有人可以将其转换为 nginx 吗?

我尝试了一些规则,但没有成功。我不知道该怎么办...来自管理面板的演示图片:https ://i.ibb.co/gwZStXN/image.png

rewrite .htaccess redirect nginx rules
  • 1 个回答
  • 404 Views
Martin Hope
Ricardo Matos
Asked: 2022-03-01 05:07:54 +0800 CST

通过 VPN 接口重定向特定流量

  • 0

我,

我想通过特定接口重定向特定端口。

直到现在我完成的是

 cat /etc/iproute2/rt_tables | grep "200 force.route" > /dev/null
 if [ $? != 0 ]; then
   echo "200       force.route" >> /etc/iproute2/rt_tables
 fi

 ip rule del from all fwmark 200 table force.route
 ip rule add from all fwmark 200 table force.route
 ip route del 0.0.0.0/1 via 92.240.245.1 dev tun_02 table force.route
 ip route add 0.0.0.0/1 via 92.240.245.1 dev tun_02 table force.route
 ip route flush cache
 iptables -A OUTPUT -t mangle -o br0 -p icmp -j MARK --set-mark 200

但....

当我 ping 包通过特定设备时,请参阅

 tcpdump -i tun_02

然后,期待响应,但我没有对 echo-r​​equest 的响应。

我怎样才能做到这一点?

到现在为止是我完成的。

vpn redirect iptables interface iproute2
  • 1 个回答
  • 69 Views
Martin Hope
Lunartist
Asked: 2022-01-28 18:05:23 +0800 CST

Nginx 重定向到 http://localhost

  • 0

情况简图

  • 我无法使用域地址,server_name因为我无法控制 DNS 服务器。我必须使用公共 IP 连接到我的 Web 服务器。
  • 所以我设置server_name为_;,但是当我请求http://firewall-public-ip:5000它重定向到http://localhost:5000.
  • 我通常可以打开其他不使用重定向的页面。例如,我可以访问http://firewall-public-ip:5000/login并登录,但它会重定向到,http://localhost:5000/login因为登录页面在登录后使用重定向。

nginx.conf:

# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 4096;
    proxy_hide_header X-Powered-By;
    proxy_hide_header Server;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;

server {
    listen 5000;

    server_name _;
    server_name_in_redirect off;
    ssl_protocols TLSv1.2;

    location '/' {
        proxy_pass http://unix:/var/sockets/gunicorn.sock;
    }
  }
}

我该如何解决?同样,我不能使用此服务器的域地址。

*EDIT 添加了应用程序重定向

@blueprint.route('/')
def route_default():
    return redirect(url_for('authentication_blueprint.login'))


@blueprint.route('/login', methods=['GET', 'POST'])
def login():
    login_form = LoginForm(request.form)
    if 'login' in request.form:

        # read form data
        username = request.form['username']
        password = request.form['password']

        # Locate user
        user = Users.query.filter_by(username=username).first()

        # Check the password
        if user and verify_pass(password, user.password):

            login_user(user)
            return redirect(url_for('authentication_blueprint.route_default'))

        # Something (user or pass) is not ok
        return render_template('accounts/login.html',
                               msg='Wrong user or password',
                               form=login_form)

    if not current_user.is_authenticated:
        return render_template('accounts/login.html',
                               form=login_form)
    return redirect(url_for('home_blueprint.index'))

apps.authentication.__init__.py

from flask import Blueprint

blueprint = Blueprint(
    'authentication_blueprint',
    __name__,
    url_prefix=''
)
redirect nginx gunicorn proxypass
  • 1 个回答
  • 434 Views
Martin Hope
TravelWhere
Asked: 2022-01-16 18:59:25 +0800 CST

如果通过浏览器直接访问图像文件,如何重定向用户?[nginx]

  • 0

如果用户尝试仅在浏览器中直接访问图像文件,我该如何重定向?我想保留嵌入图像的能力<img src="...">。如何从重定向

https://img.example.com/c/600x1200_90_webp/img-master/img/2022/01/03/08/04/54/95259215_p0_master1200.jpg

至

https://example.com/detail?id=95259215

这是我的 nginx 配置文件

location ~ "^/c/600x1200_90_webp/img-master/img/\d+/\d+/\d+/\d+/\d+/\d+/(?<filename>.+\.(jpg|png|webp))$" {
    return 301 https://example.com/detail?id=$filename;
}

代码不起作用,因为它会起作用,https://example.com/detail?id=95259215_p0_master1200.jpg但我需要它在文件名的最后一位数字之后修剪字符串,所以在这种情况下 trim off _p0_master1200.jpg。如果用户通过浏览器访问它,我也不知道如何制作它。

rewrite redirect nginx
  • 1 个回答
  • 264 Views
Martin Hope
Scott Walter
Asked: 2022-01-15 06:19:01 +0800 CST

从 nginx url 中删除井号 (#) 并重定向

  • 2

我想获取一个 url,添加一个前缀以及从 url 中删除 hashmark。例如,从 http://localhost:8088/app1/#/hello 重定向到 http://localhost:8088/apps/app1/hello。

我已经尝试过了,但它似乎不起作用:

  location /app1/ {
    rewrite ^([^#]*)([#])\/(.*) /apps$1$3 permanent;
  }

它似乎与井号有关。我可以从 http://localhost:8088/app1/q/hello 到 http://localhost:8088/apps/app1/hello 使用这个:

  location /app1/ {
    rewrite ^([^q]*)([q])\/(.*) /apps$1$3 permanent;
  }

我需要做些什么来处理重定向中的井号吗?

redirect nginx
  • 1 个回答
  • 1111 Views
Martin Hope
user3553828
Asked: 2022-01-03 09:06:08 +0800 CST

如何在 Nginx 中重定向带有特殊 (%C2%A0) 字符的 URL

  • 1

我发现我有一个链接到一个带有一些垃圾的 URL 后面会产生一些流量,但是无论它放置它的人都做错了:

/domain/sub-directory/%C2%A0

因此,我想将此 URL 重定向到预期的 URL。但是,我无法弄清楚如何正确执行此操作。

我已经试过了:

location ^~ /sub-directory/(.*) { return 301 https://example.com/sub-directory/; }

并且

rewrite ^https://example.com/sub-directory/%C2%A0$ https://example.com/sub-directory/ permanent;

但它们都不起作用。任何想法,如何解决这个问题?

redirect nginx
  • 1 个回答
  • 212 Views
Martin Hope
AtomX
Asked: 2021-11-12 10:42:35 +0800 CST

如何解决非 www 重定向到 www Nginx?

  • 0

这是我的 NGinx Web 服务器配置

    server {
        if ($host ~ ^[^.]+\.betafox\.net$) {
            return 301 https://$host$request_uri;
        } # managed by Certbot
    
    
        if ($host = www.betafox.net) {
            return 301 https://$host$request_uri;
        } # managed by Certbot
    
    
        if ($host = betafox.net) {
            return 301 https://$host$request_uri; 
        } # managed by Certbot
    
        listen 80;
        listen [::]:80;
    
        #server_name _;
        root /var/www/html;
    
    server_name betafox.net *.betafox.net;
        #return 301 https://$host$request_uri;
        index index.php index.html index.htm;
        location / {
            # try_files $uri $uri/ =404;
            try_files $uri $uri/ /index.php?q=$uri&$args;
            proxy_pass  https://betafox.net/;
            proxy_redirect  https://betafox.net/ $host;
            proxy_set_header Accept-Encoding "";
            proxy_ssl_server_name on;
        }
        location ~ \.php$ {
            include snippets/fastcgi-php.conf;
            #fastcgi_pass 127.0.0.1:9000;
            #fastcgi_pass unix:/run/php/php8.0-fpm.sock;
            fastcgi_pass unix:/etc/alternatives/php-fpm.sock;
     }
    
    
    }
    
    server {
    
    listen 443 ssl default_server;
            listen [::]:443 ssl default_server;
    
            root /var/www/html;
            index index.php index.html index.htm;
    
          # server_name _;
            server_name betafox.net *.betafox.net;
            # Maximum file upload size is 4MB - change accordingly if needed
            client_max_body_size 512M;
            client_body_buffer_size 128k;
            include snippets/letsencrypt-nginx-certs.conf;
            include snippets/letsencrypt-nginx-route.conf;
    
            location / {
                    # try_files $uri $uri/ =404;
                    try_files $uri $uri/ /index.php?q=$uri&$args;
            }
    error_page 404 /404.html;
    
            error_page 500 502 503 504 /50x.html;
            location = /50x.html {
                    root /usr/share/nginx/html;
            }
    
            location ~ \.php$ {
                    include snippets/fastcgi-php.conf;
                    #fastcgi_pass 127.0.0.1:9000;
                    #fastcgi_pass unix:/var/run/php8.0-fpm.sock;
                    fastcgi_pass unix:/etc/alternatives/php-fpm.sock;
            }
        ssl_certificate /etc/letsencrypt/live/betafox.net-0001/fullchain.pem; # managed by Certbot
        ssl_certificate_key /etc/letsencrypt/live/betafox.net-0001/privkey.pem; # managed by Certbot
    
    }

当我为我的 FQDN 和子域安装 SSL 证书时,Certbot 自动修改了大部分内容。我面临的问题是关于 URL 重定向。原始 URL 是www.betafox.net,当用户键入 betafox.net 时,将重定向到https://betafoxnet.www.betafox.net/并且有消息说:您要查找的站点不存在。

我只希望所有键入 betafox.net 的用户都被转发到www.betafox.net。我相信 Nginx 可以做到这一点。我怎样才能做到这一点?

linux redirect nginx
  • 1 个回答
  • 56 Views

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    新安装后 postgres 的默认超级用户用户名/密码是什么?

    • 5 个回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve