将位置添加到 nginx 中的 URL 路径

发布于 2025-01-11 01:23:49 字数 601 浏览 0 评论 0原文

Nginx 配置:

server {
        listen 443 ssl;
        server_name xyx.com;


    location /name/ {
        rewrite    /name/([^/]+) /users?name=$1 break;
        proxy_pass http://127.0.0.1:80/;
}
} 

如果只有 location /,上面的链接就可以正常工作,但是如果使用像上面 location /name/ 这样的任何其他路径,它就会失败。

我们希望在请求时位置路径成为 url 的一部分。

所有 xyz.com/name url(及相关)应将用户代理/重定向到 http://127.0.0.1: 80/name

尝试了 proxy_pass http://127.0.0.1$request_uri 和其他一些东西,包括重写,但它不起作用。

任何建议表示赞赏 - 谢谢。

Nginx config:

server {
        listen 443 ssl;
        server_name xyx.com;


    location /name/ {
        rewrite    /name/([^/]+) /users?name=$1 break;
        proxy_pass http://127.0.0.1:80/;
}
} 

The above link works well if its just location / but with any other path like above location /name/ it fails.

We want the location path to be part of the url when requested.

All xyz.com/name url (and dependent) should proxy/redirect the users to http://127.0.0.1:80/name

Tried proxy_pass http://127.0.0.1$request_uri and other few stuff including rewrite and it didn't work.

Any suggestions appreciated - thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

街角卖回忆 2025-01-18 01:23:49
server {
    ......
    ...... stuff

    location / {
        proxy_pass http://Client;
        proxy_buffering off;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-Host $host;
        proxy_set_header X-Forwarded-Port $server_port;

        fastcgi_buffers 16 16k;
        fastcgi_buffer_size 32k;

    }

    location /identity/ {
            proxy_pass http://Identity/;

            proxy_buffering off;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-Host $host;
            proxy_set_header X-Forwarded-Port $server_port;

            fastcgi_buffers 16 16k;
            fastcgi_buffer_size 32k;

    }

}

upstream Client{
        zone Client 64k;
        server localhost:5001;
}



upstream Identity{

        zone Identity 64k;
        server localhost:9001;
}

example.com ===>我的客户项目
example.com/identity ===>我的身份项目

注意:有时您可能需要在项目中间件管道中定义原始 URL。就我而言,我配置身份服务器并生成网址。我必须在我的中间件管道中添加以下内容,以了解您的原始基本网址是 example.com/identity/,因此网址必须基于此基本网址生成。(.net core)

app.Use(async (ctx, next) =>
{
    ctx.Request.Scheme = "https";
    ctx.Request.Host = new HostString("example.com/identity/");
    
    await next();
});
server {
    ......
    ...... stuff

    location / {
        proxy_pass http://Client;
        proxy_buffering off;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-Host $host;
        proxy_set_header X-Forwarded-Port $server_port;

        fastcgi_buffers 16 16k;
        fastcgi_buffer_size 32k;

    }

    location /identity/ {
            proxy_pass http://Identity/;

            proxy_buffering off;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-Host $host;
            proxy_set_header X-Forwarded-Port $server_port;

            fastcgi_buffers 16 16k;
            fastcgi_buffer_size 32k;

    }

}

upstream Client{
        zone Client 64k;
        server localhost:5001;
}



upstream Identity{

        zone Identity 64k;
        server localhost:9001;
}

example.com ===> my client project
example.com/identity ===> my identity project

NOTE: some times you may have to define the original url in your project middleware pipeline. in my case i configure identity server and it generates urls. i have to add below in my middleware pipeline to understood it that your origin base url is example.com/identity/ so the urls must generate base on this base url.(.net core)

app.Use(async (ctx, next) =>
{
    ctx.Request.Scheme = "https";
    ctx.Request.Host = new HostString("example.com/identity/");
    
    await next();
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文