经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 数据库/运维 » Nginx » 查看文章
nginx中的两个模块的proxy_pass的区别解析
来源:jb51  时间:2021/11/29 12:51:42  对本文有异议

1.ngx_stream_proxy_module模块的proxy_pass指令

只能在server段使用使用, 只需要提供域名或ip地址和端口。

可以理解为端口转发,可以是tcp端口,也可以是udp端口。

  1. server {
  2. listen 127.0.0.1:12345;
  3. proxy_pass 127.0.0.1:8080;
  4. }
  5. server {
  6. listen 12345;
  7. proxy_connect_timeout 1s;
  8. proxy_timeout 1m;
  9. proxy_pass example.com:12345;
  10. }
  11. server {
  12. listen 53 udp;
  13. proxy_responses 1;
  14. proxy_timeout 20s;
  15. proxy_pass dns.example.com:53;
  16. }
  17. server {
  18. listen [::1]:12345;
  19. proxy_pass unix:/tmp/stream.socket;

2.ngx_http_proxy_module模块的proxy_pass指令

  • 需要在location段,location中的if段,limit_except段中使用,
  • 处理需要提供域名或ip地址和端口外,还需要提供协议,如"http"或"https",
  • 还有一个可选的uri可以配置。

3.proxy_pass后,后端服务器的url(request_uri)情况分析

  1. server {
  2. listen 80;
  3. server_name www.test.com;
  4. # 情形A
  5. # 访问 http://www.test.com/testa/aaaa
  6. # 后端的request_uri为: /testa/aaaa
  7. location ^~ /testa/ {
  8. proxy_pass http://127.0.0.1:8801;
  9. }
  10. # 情形B
  11. # 访问 http://www.test.com/testb/bbbb
  12. # 后端的request_uri为: /bbbb
  13. location ^~ /testb/ {
  14. proxy_pass http://127.0.0.1:8801/;
  15. }
  16. # 情形C
  17. # 下面这段location是正确的
  18. location ~ /testc {
  19. proxy_pass http://127.0.0.1:8801;
  20. }
  21. # 情形D
  22. # 下面这段location是错误的
  23. #
  24. # nginx -t 时,会报如下错误:
  25. #
  26. # nginx: [emerg] "proxy_pass" cannot have URI part in location given by regular
  27. # expression, or inside named location, or inside "if" statement, or inside
  28. # "limit_except" block in /opt/app/nginx/conf/vhost/test.conf:17
  29. #
  30. # 当location为正则表达式时,proxy_pass 不能包含URI部分。本例中包含了"/"
  31. location ~ /testd {
  32. proxy_pass http://127.0.0.1:8801/; # 记住,location为正则表达式时,不能这样写!!!
  33. }
  34. # 情形E
  35. # 访问 http://www.test.com/ccc/bbbb
  36. # 后端的request_uri为: /aaa/ccc/bbbb
  37. location /ccc/ {
  38. proxy_pass http://127.0.0.1:8801/aaa$request_uri;
  39. }
  40. # 情形F
  41. # 访问 http://www.test.com/namea/ddd
  42. # 后端的request_uri为: /yongfu?namea=ddd
  43. location /namea/ {
  44. rewrite /namea/([^/]+) /yongfu?namea=$1 break;
  45. proxy_pass http://127.0.0.1:8801;
  46. }
  47. # 情形G
  48. # 访问 http://www.test.com/nameb/eee
  49. # 后端的request_uri为: /yongfu?nameb=eee
  50. location /nameb/ {
  51. rewrite /nameb/([^/]+) /yongfu?nameb=$1 break;
  52. proxy_pass http://127.0.0.1:8801/;
  53. }
  54. access_log /data/logs/www/www.test.com.log;
  55. }
  56. server {
  57. listen 8801;
  58. server_name www.test.com;
  59. root /data/www/test;
  60. index index.php index.html;
  61. rewrite ^(.*)$ /test.php?u=$1 last;
  62. location ~ \.php$ {
  63. try_files $uri =404;
  64. fastcgi_pass unix:/tmp/php-cgi.sock;
  65. fastcgi_index index.php;
  66. include fastcgi.conf;
  67. }
  68. access_log /data/logs/www/www.test.com.8801.log;
  69. }

到此这篇关于nginx中的两个模块的proxy_pass的区别的文章就介绍到这了,更多相关nginx proxy_pass模块内容请搜索w3xue以前的文章或继续浏览下面的相关文章希望大家以后多多支持w3xue!

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号