千家信息网

Nginx中location和proxy_pass路径如何配置

发表于:2026-10-31 作者:千家信息网编辑
千家信息网最后更新 2026年10月31日,小编给大家分享一下Nginx中location和proxy_pass路径如何配置,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下
千家信息网最后更新 2026年10月31日Nginx中location和proxy_pass路径如何配置

小编给大家分享一下Nginx中location和proxy_pass路径如何配置,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!

    一、Nginx location 基本配置

    1.1、Nginx 配置文件

    upstream test1{server 127.0.0.1:8000;}upstream test2{server 127.0.0.1:8000;}server{        server_name  test.com;        listen 80;        access_log /usr/local/openresty/nginx/logs/test.com_access.log latest;        error_log  /usr/local/openresty/nginx/logs/test.com.log error;        proxy_set_header   Host             $host;        proxy_set_header   X-Real-IP        $remote_addr;        proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;        proxy_connect_timeout   3s;        proxy_read_timeout 120s;        proxy_send_timeout 120s;        proxy_next_upstream error timeout invalid_header http_404 http_502 http_504 http_500;                location /user/ {                        proxy_set_header Connection "";                proxy_http_version 1.1;                        proxy_pass http://test1/;                }        location / {                proxy_set_header Connection "";                proxy_http_version 1.1;                proxy_pass http://test2/;        }}

    1.2 、Python 脚本

    python2 可以运行

    该脚本用于获取请求内容。 这个作为后端,也就是 proxy_pass 代理的后端。

    #!/usr/bin/env pythonimport SimpleHTTPServerimport SocketServerPORT = 8000class GetHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):    def do_GET(self):        print(self.headers)        self.send_response(200, "")    def do_POST(self):        print(self.headers)        content_length = self.headers.getheaders('content-length')        length = int(content_length[0]) if content_length else 0        print(self.rfile.read(length))        self.send_response(200, "")Handler = GetHandlerhttpd = SocketServer.TCPServer(("", PORT), Handler)httpd.serve_forever()

    二、测试

    2.1、测试 location

    末尾存在 / 和 proxy_pass末尾存在 /

    nginx配置如下

     location /user/ {                        proxy_set_header Connection "";                proxy_http_version 1.1;                        proxy_pass http://test1/;                }

    请求url

    test.com/user/test.html

    后端内容

    打印的内容:

    Host: test1Content-Length: 0User-Agent: PostmanRuntime/7.26.8Accept: */*Postman-Token: f2bfe770-4f44-4ee9-91c4-060f59dfb26cAccept-Encoding: gzip, deflate, br127.0.0.1 - - [10/Apr/2021 16:54:26] "POST /test.html HTTP/1.1" 200 -

    小结论:proxy_pass 地址加了 / 的话, 请求 test.com/user/test.html 实际请求是 http://test1/test.html。

    2.2、测试 location

    末尾存在 / 和 proxy_pass末尾不存在 /

    nginx配置如下

     location /user/ {                        proxy_set_header Connection "";                proxy_http_version 1.1;                        proxy_pass http://test1;                }

    请求url

    test.com/user/test.html

    后端内容

    打印的内容:

    Host: test1
    Content-Length: 0
    User-Agent: PostmanRuntime/7.26.8
    Accept: */*
    Postman-Token: e33d0a2c-1965-4152-b87c-94fca50f2899
    Accept-Encoding: gzip, deflate, br

    127.0.0.1 - - [10/Apr/2021 16:57:18] "POST /user/test.html HTTP/1.1" 200 -

    小结论: proxy_pass 地址不加了 / 的话, 请求 test.com/user/test.html 实际请求是 http://test1/user/test.html

    2.3、测试三 location

    不加末尾 / 且 proxy_pass 不加 末尾 /

    nginx配置如下

     location /user {                        proxy_set_header Connection "";                proxy_http_version 1.1;                        proxy_pass http://test1;                }

    请求url

    test.com/user/test.html

    后端内容

    打印的内容:

    Host: test1
    Content-Length: 0
    User-Agent: PostmanRuntime/7.26.8
    Accept: */*
    Postman-Token: 31cd33c6-4c95-41b5-a095-28cdc7113dcd
    Accept-Encoding: gzip, deflate, br

    127.0.0.1 - - [10/Apr/2021 16:59:34] "POST /user/test.html HTTP/1.1" 200 -

    请求 test.com/user/test.html 实际请求是 http://test1/user/test.html

    2.4、location 不加

    末尾 / 且 proxy_pass 加 末尾 /

    nginx配置如下

      location /user {                        proxy_set_header Connection "";                proxy_http_version 1.1;                        proxy_pass http://test1/;                }

    请求url

    test.com/user/test.html

    后端内容

    打印的内容:

    Host: test1
    Content-Length: 0
    User-Agent: PostmanRuntime/7.26.8
    Accept: */*
    Postman-Token: d0f4b83f-6482-41ba-8a01-c059eececc2d
    Accept-Encoding: gzip, deflate, br

    127.0.0.1 - - [10/Apr/2021 17:00:21] "POST //test.html HTTP/1.1" 200 -

    请求 test.com/user/test.html 实际请求是 http://test1//test.html

    2.5、location 末尾

    有 / proxy_pass 末尾其他有路径,且末尾加 /

    nginx配置如下

       location /user/ {                        proxy_set_header Connection "";                proxy_http_version 1.1;                        proxy_pass http://test1/haha/;                }

    请求url

    test.com/user/test.html

    后端内容

    打印的内容:

    Host: test1
    Content-Length: 0
    User-Agent: PostmanRuntime/7.26.8
    Accept: */*
    Postman-Token: 6447cf0b-5988-4f96-81a4-2b621fe32604
    Accept-Encoding: gzip, deflate, br

    127.0.0.1 - - [10/Apr/2021 17:03:27] "POST /haha/test.html HTTP/1.1" 200 -

    请求 test.com/user/test.html 实际请求是 http://test1/haha/test.html

    2.6、 location 末尾

    有 / proxy_pass 末尾其他有路径,且末尾不加 /

    nginx配置如下

     location /user/ {                        proxy_set_header Connection "";                proxy_http_version 1.1;                        proxy_pass http://test1/haha;                }

    请求url

    test.com/user/test.html

    后端内容

    打印的内容:

    Host: test1
    Content-Length: 0
    User-Agent: PostmanRuntime/7.26.8
    Accept: */*
    Postman-Token: 32fb2a50-1e7c-4131-9804-1828e21ca841
    Accept-Encoding: gzip, deflate, br

    127.0.0.1 - - [10/Apr/2021 17:05:03] "POST /hahatest.html HTTP/1.1" 200 -

    请求 test.com/user/test.html 实际请求是 http://test1/hahatest.html

    三、总结








    序号访问URLlocation配置proxy_pass配置后端接收的请求备注
    1test.com/user/test.html/user/http://test1//test.html
    2test.com/user/test.html/user/http://test1/user/test.html
    3test.com/user/test.html/userhttp://test1/user/test.html
    4test.com/user/test.html/userhttp://test1///test.html
    5test.com/user/test.html/user/http://test1/haha//haha/test.html
    6test.com/user/test.html/user/http://test1/haha/hahatest.html


    注意上表格中的后端是指 python 脚本对应的web服务。

    在日常的web网站部署中,经常会用到 nginx的 proxy_pass 反向代理,有一个配置需要弄清楚:配置 proxy_pass 时,

    • 当在后面的 upstram_name 后面出现了 /,相当于是绝对根路径,则 nginx 不会把 location 中匹配的路径部分代理走;

    • 如果没有 /,则会把匹配的路径部分也给代理走。

    以上是"Nginx中location和proxy_pass路径如何配置"这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注行业资讯频道!

    内容 配置 末尾 路径 实际 求是 代理 测试 篇文章 脚本 地址 结论 部分 不怎么 也就是 基本配置 备注 大部分 常会 序号 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 互联网科技 今日互联网科技 河南真二互联网科技有限公司 工业互联网科技新基建 美国互联网科技 广州互联网络科技有限公司 米客互联网科技 碳银互联网科技有限公司 无锡溥仪互联网科技有限公司 互联网科技博主是什么意思 互联网与医疗科技 济南有互联网科技公司吗 四川互联网展示科技馆设计 半导体行业属于互联网高科技类吗 西安有哪些互联网高科技企业 昆明中奇联合互联网科技 陕西彩京云互联网科技有限公司 平安好医生打造互联网科技医疗 青岛互联网科技园 银川新华互联网科技学校招聘 互联网科技工作委员会 云信互联网科技有限公司 栖莱科技互联网股份有限公司 国大互联网教育科技有限公司工作 浙南科技城互联网大厦建设工程 江苏互联网智能科技软件 上海创道互联网络科技有限公司 求路客互联网科技电话 第三届互联网大会安防黑科技 盐城市攀科互联网科技有限公司
    0