nginx be forwarded by the custom header objects

Original link: https: //www.cnblogs.com/xiao987334176/p/11263649.html

First, the custom header header

Because the need to release the gray line, as long as the head is received nginx:

x_app:123456

 

It will jump to another url, such as:

1.0.0.10:8080

 

Be forwarded to the environment by arranging the gradation nginx matching request header wx_unionid.
Core: The client http header custom in nginx configuration file can be read directly.
Conditions: header must minus "-" separate words, nginx which will be converted into a corresponding underscore "_" in lowercase word connection.

 

Second, modify Nginx configuration

Install nginx

apt-get install -y nginx

 

Edit Home

cd /etc/nginx/sites-enabled
vim home.conf

It reads as follows:

Copy the code
upstream wx {
    server 127.0.0.1:8080;
}

server {  
    listen 8008; server_name localhost; root html; index index.html; charset utf-8; underscores_in_headers on; location / { #测试header转发 if ($http_x_app = "123456") { proxy_pass http://wx;  } } } 
Copy the code

Parameter configuration instructions

underscores_in_headers on: nginx nginx is used to read non-standard user-defined header, but underlined the need to support open header or in the http server:
such as our custom header as wx_unionid, the time required to obtain the header like this: $ http_wx_unionid (all lowercase, and more a front HTTP_)


If you need to custom header passed to the next nginx:
1. If it is the custom in nginx X_CUSTOM_HEADER $ HTTP_HOST employed proxy_set_header;
2. If the header is requested when the user-defined, e.g. curl -head -H "X_CUSTOM_HEADER: foo "http://domain.com/api/test, you need to pass through proxy_pass_header X_CUSTOM_HEADER

Page editorial adjustments

I came wx.conf

It reads as follows:

Copy the code
server {
    listen 8080;
    server_name localhost;

    root /var/www/html;
    index wx.html;

    location / {
        try_files $uri $uri/ =404; } }
Copy the code

 

Increase the test page

vim /var/www/html/wx.html

It reads as follows:

<H1> micro-channel test platform applet </ h1>

 

Third, the test results

Add custom header

Copy the code
root@ubuntu:~# curl -v -H 'wx_unionid:123456' 127.0.0.1:8008
* Rebuilt URL to: 127.0.0.1:8008/
*   Trying 127.0.0.1... * Connected to 127.0.0.1 (127.0.0.1) port 8008 (#0) > GET / HTTP/1.1 > Host: 127.0.0.1:8008 > User-Agent: curl/7.47.0 > Accept: */* > wx_unionid:123456 > < HTTP/1.1 200 OK < Server: nginx/1.10.3 (Ubuntu) < Date: Mon, 29 Jul 2019 07:49:46 GMT < Content-Type: text/html; charset=utf-8 < Content-Length: 37 < Connection: keep-alive < Last-Modified: Mon, 29 Jul 2019 06:23:49 GMT < ETag: "5d3e90f5-25" < Accept-Ranges: bytes < <h1>微信小程序测试平台</h1> * Connection #0 to host 127.0.0.1 left intact
Copy the code

 

Without head

Copy the code
root@ubuntu:~# curl -v 127.0.0.1:8008
* Rebuilt URL to: 127.0.0.1:8008/
*   Trying 127.0.0.1... * Connected to 127.0.0.1 (127.0.0.1) port 8008 (#0) > GET / HTTP/1.1 > Host: 127.0.0.1:8008 > User-Agent: curl/7.47.0 > Accept: */* > < HTTP/1.1 200 OK < Server: nginx/1.10.3 (Ubuntu) < Date: Mon, 29 Jul 2019 07:50:13 GMT < Content-Type: text/html; charset=utf-8 < Content-Length: 612 < Last-Modified: Tue, 31 Jan 2017 15:01:11 GMT < Connection: keep-alive < ETag: "5890a6b7-264" < Accept-Ranges: bytes < <!DOCTYPE html> <html> <head> <title>Welcome to nginx!</title> <style> body { width: 35em; margin: 0 auto; font-family: Tahoma, Verdana, Arial, sans-serif; } </style> </head> <body> <h1>Welcome to nginx!</h1> <p>If you see this page, the nginx web server is successfully installed and working. Further configuration is required.</p> <p>For online documentation and support please refer to <a href="http://nginx.org/">nginx.org</a>.<br/> Commercial support is available at <a href="http://nginx.com/">nginx.com</a>.</p> <p><em>Thank you for using nginx.</em></p> </body> </html> * Connection #0 to host 127.0.0.1 left intact
Copy the code

 

Fourth, the multi-matching condition

Now one more demand, the need for the client to ip: 192.168.0.45, but also add custom header wx_unionid: 123456, will be forwarded, forwarded or not.

 

nginx configuration not supported if the logical condition || or && Logical operation, and does not support nested syntax if, otherwise errors will be reported below: nginx: [emerg] invalid condition .

We can use indirect ways to achieve variable.

To achieve the statement:

if ($remote_addr ~* "192.168.0.45" && $http_wx_unionid = "123456"){ proxy_pass http://wx; }

 

If configured in accordance with this, it will be reported nginx: [emerg] invalid condition error.

as follows:

nginx: [emerg] invalid condition "$http_wx_unionid" in /etc/nginx/sites-enabled/home.conf:16 nginx: configuration file /etc/nginx/nginx.conf test failed

 

It can be implemented as follows:

Copy the code
{WX upstream 
    Server . 127.0 0.1: 8080 ;  } 
 Server {  the listen 8008 ; server_name localhost; the root HTML; index.html index; charset UTF- . 8 ; underscores_in_headers ON; LOCATION / {# Test # header forwarding $ In Flag flag SET 0 ; IF (* ~ $ REMOTE_ADDR " 192.168.0.45 " ) appended. {# 1, then In Flag = $ 01 SET In Flag $ " $ {1} In Flag " ;} IF (http_wx_unionid $ = " 123456 " ) {# 1 appending In this case In Flag = $ 011 SET In Flag $ " $ {}. 1 In Flag ";} When # 011, represents the conditions are matched IF (In Flag $ = " 011 " ) {proxy_pass HTTP: // WX; }}}
Copy the code

 

Reload Configuration

nginx -s reload

 

Using the machine tested again, increase the head

Copy the code
root@ubuntu:~# curl -v -H 'wx_unionid:123456' 127.0.0.1:8008 * Rebuilt URL to: 127.0.0.1:8008/ * Trying 127.0.0.1... * Connected to 127.0.0.1 (127.0.0.1) port 8008 (#0) > GET / HTTP/1.1 > Host: 127.0.0.1:8008 > User-Agent: curl/7.47.0 > Accept: */* > wx_unionid:123456 > < HTTP/1.1 200 OK < Server: nginx/1.10.3 (Ubuntu) < Date: Mon, 29 Jul 2019 08:01:30 GMT < Content-Type: text/html; charset=utf-8 < Content-Length: 612 < Last-Modified: Tue, 31 Jan 2017 15:01:11 GMT < Connection: keep-alive < ETag: "5890a6b7-264" < Accept-Ranges: bytes < <!DOCTYPE html> <html> <head> <title>Welcome to nginx!</title> <style> body { width: 35em; margin: 0 auto; font-family: Tahoma, Verdana, Arial, sans-serif; } </style> </head> <body> <h1>Welcome to nginx!</h1> <p>If you see this page, the nginx web server is successfully installed and working. Further configuration is required.</p> <p>For online documentation and support please refer to <a href="http://nginx.org/">nginx.org</a>.<br/> Commercial support is available at <a href="http://nginx.com/">nginx.com</a>.</p> <p><em>Thank you for using nginx.</em></p> </body> </html> * Connection #0 to host 127.0.0.1 left intact
Copy the code

 

Log on to 192.168.0.45, increase test head

Copy the code
root@docker-reg:~# curl -v -H 'wx_unionid:123456' 192.168.0.162:8008 * Rebuilt URL to: 192.168.0.162:8008/ * Trying 192.168.0.162... * Connected to 192.168.0.162 (192.168.0.162) port 8008 (#0) > GET / HTTP/1.1 > Host: 192.168.0.162:8008 > User-Agent: curl/7.47.0 > Accept: */* > wx_unionid:123456 > < HTTP/1.1 200 OK < Server: nginx/1.10.3 (Ubuntu) < Date: Mon, 29 Jul 2019 08:02:01 GMT < Content-Type: text/html; charset=utf-8 < Content-Length: 37 < Connection: keep-alive < Last-Modified: Mon, 29 Jul 2019 06:23:49 GMT < ETag: "5d3e90f5-25" < Accept-Ranges: bytes < <h1>微信小程序测试平台</h1> * Connection #0 to host 192.168.0.162 left intact
Copy the code

Guess you like

Origin www.cnblogs.com/kaishirenshi/p/12120639.html