In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces the relevant knowledge of how to use proxy_pass in Nginx, the content is detailed and easy to understand, the operation is simple and fast, and it has a certain reference value. I believe you will gain something after reading this article on how to use proxy_pass in Nginx. Let's take a look at it.
1. Proxy_pass 's official guide to nginx
There are two modules in nginx that have proxy_pass instructions.
Proxy_pass of ngx_http_proxy_module:
Syntax: proxy_pass URL; scenario: location, if in location, limit_except description: set the protocol (protocol) and address (address) of the back-end proxy server, and an optional URI that can be matched in location. The protocol can be "http" or "https". The address can be a domain name or ip address and port, or a unix-domain socket path. For details, see the official document: http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_passURI matching, which is discussed in the fourth part of this article.
Proxy_pass of ngx_stream_proxy_module:
Syntax: proxy_pass address; scenario: server description: set the address of the backend proxy server. This address (address) can be a domain name or ip address and port, or a unix-domain socket path. For details, see the official document: the relationship and difference between http://nginx.org/en/docs/stream/ngx_stream_proxy_module.html#proxy_pass II and two proxy_pass
In both modules, both proxy_pass are instructions used to act as back-end agents.
The proxy_pass instruction of the ngx_stream_proxy_module module can only be used in the server segment, and only the domain name or ip address and port need to be provided. It can be understood as port forwarding, which can be a tcp port or a udp port.
The proxy_pass instruction of the ngx_http_proxy_module module needs to be used in the location segment, the if segment in the location, and the limit_except segment. The processing needs to provide not only a domain name or ip address and port, but also a protocol, such as "http" or "https", and an optional uri can be configured.
III. The specific usage of proxy_pass
Proxy_pass instruction of ngx_stream_proxy_module module
Server {listen 127.0.0.1 proxy_pass 12345; proxy_pass 127.0.1 proxy_pass 8080;} server {listen 12345; proxy_connect_timeout 1s; proxy_timeout 1m; proxy_pass example.com:12345;} server {listen 53 udp; proxy_responses 1; proxy_timeout 20s; proxy_pass dns.example.com:53;} server {listen [:: 1]: 12345; proxy_pass unix:/tmp/stream.socket;}
Proxy_pass instruction of ngx_http_proxy_module module
Server {listen 80; server_name www.test.com; # normal proxy, do not modify the backend url location / some/path/ {proxy_pass http://127.0.0.1;} # modify the backend url address proxy (in this case, the backend address is ended with a slash) location / testb {proxy_pass http://www.other.com:8801/; } # using if in location location / google {if ($geoip_country_code ~ (RU | CN)) {proxy_pass http://www.google.hk;}} location / yongfu/ {# does not match limit_except, proxy to unix:/tmp/backend.socket:/uri/ proxy_pass http://unix:/tmp/backend.socket:/uri/;; # match to the request method: PUT or DELETE, proxy to 9080 limit_except PUT DELETE {proxy_pass http://127.0.0.1:9080;} IV. After proxy_pass, analyze the url (request_uri) of the backend server server {listen 80; server_name www.test.com # scenario A # the request_uri of accessing the http://www.test.com/testa/aaaa # backend is: / testa/aaaa location ^ ~ / testa/ {proxy_pass http://127.0.0.1:8801; } # scenario B # accesses the request_uri of the http://www.test.com/testb/bbbb # backend: / bbbb location ^ ~ / testb/ {proxy_pass http://127.0.0.1:8801/;} # the location below scenario C # is the correct location ~ / testc {proxy_pass http://127.0.0.1:8801; } # in case D # the following location is incorrect # # nginx-t, the following error will be reported: # # nginx: [emerg] "proxy_pass" cannot have URI part in location given by regular # expression, or inside named location, or inside "if" statement, or inside # "limit_except" block in / opt/app/nginx/conf/vhost/test.conf:17 # # when location is a regular expression Proxy_pass cannot contain a URI section. This example includes "/" location ~ / testd {proxy_pass http://127.0.0.1:8801/; # remember, you can't write this when location is a regular expression! } # case E # accesses the request_uri of http://www.test.com/ccc/bbbb # backend: / aaa/ccc/bbbb location / ccc/ {proxy_pass http://127.0.0.1:8801/aaa$request_uri; } # case F # accesses the request_uri of http://www.test.com/namea/ddd # backend: / yongfu?namea=ddd location / namea/ {rewrite / namea/ ([^ /] +) / yongfu?namea=$1 break; proxy_pass http://127.0.0.1:8801; } # in case G # accesses the request_uri of http://www.test.com/nameb/eee # backend: / yongfu?nameb=eee location / nameb/ {rewrite / nameb/ ([^ /] +) / yongfu?nameb=$1 break; proxy_pass http://127.0.0.1:8801/;} access_log / data/logs/www/www.test.com.log;} server {listen 8801 Server_name www.test.com; root / data/www/test; index index.php index.html; rewrite ^ (. *) $/ test.php?u=$1 last; location ~\ .php$ {try_files $uri = 404; fastcgi_pass unix:/tmp/php-cgi.sock; fastcgi_index index.php; include fastcgi.conf;} access_log / data/logs/www/www.test.com.8801.log;}
File: / data/www/test/test.php
Echo'$_ server [request _ URI]:'. $_ SERVER ['REQUEST_URI']
By looking at the value of $_ SERVER ['REQUEST_URI'], we can see the value of the backend request_uri for each request for verification.
This is the end of the article on "how to use proxy_pass in Nginx". Thank you for reading! I believe you all have a certain understanding of the knowledge of "how to use proxy_pass in Nginx". If you want to learn more, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.