nginx使用记录问题:记录nginx.conf配置等问题

nginx个人使用记录,仅供参考哈,碰到问题更新中······

1、个人使用场景

场景1:使用rewrite 实现URL重写和重定向

需求1:服务器端实现URL重写和重定向,使用 rewrite
大白话:需要访问http://ip:port/aaa/bbb,访问到后端url为:http://ip:port/bbb

		location ^~ /aaa/bbb/ {
            proxy_pass http://ccc:18000;
        }

以上代码块中nginx做服务器
访问http://xxx:port/aaa/bbb/,会被nginx匹配到会转发到:http://ccc:18000/aaa/bbb
如果实际访问到后端得不加/aaa,需要添加rewrite
如以下代码块:访问http://xxx:port/aaa/bbb/,nginx实际访问后端地址为:http://ccc:18000/bbb

		location ^~ /aaa/bbb/ {
			rewrite ^/aaa/bbb/(.*)$ /bbb/$1 break;
            proxy_pass http://ccc:18000;
        }

2、nginx.conf配置文件中location详解

1)location块匹配

location [modifier] uri {
    # 配置指令集
}

modifier可选:
=: 精确匹配,优先级1
^~: 前缀匹配,优先级2
~ 和 ~*: 正则表达式匹配。前者区分大小写,后者不区分大小写。优先级3

实例:

#精确匹配
location = /exact/path {
    # 对于请求 /exact/path,此location生效
}

#前缀匹配
location ^~ /images/ {
    # 对于 /images/ 开始的所有请求,如 /images/example.jpg,此location生效,
    # 并且即使有正则表达式匹配也不再考虑
}

#正则表达式匹配
location ~ \.php$ {
    # 匹配所有以.php结尾的请求,如 /path/to/script.php
}

2)location块常用内部指令

proxy_pass: 设置反向代理到后端服务器。
rewrite: 实现URL重写功能

1、不带URI的proxy_pass

location /api/ {
    proxy_pass http://backend_server:8080;
}

访问:http://xxx:port/api/some_resource
实际:http://backend_server:8080/api/some_resource

2、带URI的proxy_pass

location /api/ {
    proxy_pass http://backend_server:8080/service/;
}

访问:http://xxx:port/api/some_resource
实际:http://backend_server:8080/service/some_resource
  • 10
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
根据你提供的引用内容,错误信息 "nginx: configuration file D:\nginx-1.22.0/conf/nginx.conf test failed" 是由于nginx配置文件中的语法错误导致的。具体来说,这个错误可能是由于在对应行或者附近行的“{”前面缺少空格引起的。解决方法是在缺少空格的地方添加空格,这样就能够解决语法错误了。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [宝塔面板nginx报错 nginx: configuration file /www/server/nginx/conf/nginx.conf test failed 语法检查...](https://blog.csdn.net/zhichina/article/details/125109468)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [毕设项目:基于SpringBoot+MyBatis+mysql的飞机订票系统.zip](https://download.csdn.net/download/qq_35831906/88222799)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [nginx: [emerg] directive “location“ has no opening “{“ in D:\nginx\nginx-1.22.0/conf/nginx.conf:...](https://blog.csdn.net/bweic/article/details/125568093)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值