编程知识 cdmana.com

demand / problem

I've been making a demand recently , The general deployment model is like this :

Due to strict port restrictions ( External exposure 80 port ) So we have a server on the right nginx According to api path Acting as a reverse agent .

Because we want to follow our code with CI jenkins Integrate , So I want to find a way to see how to make jenkins Also through the proxy server 80 Port access ?

step

step1:

We will /j/ Access to jenkins Server address .  For example, the server is : test.com, So we go through test.com/j/ To visit jenkins:

location /j/ {
      proxy_pass http://127.0.0.1:8002/; # there
      rewrite ^/j/(.*)$ /$1 break;   # And rewrite to remove /j Then send it to jenkins
    }         

problem :  Found automatic redirection to test.com/login:

Step2:

add to sub_filter To rewrite content:

location /j/ {
      proxy_pass http://127.0.0.1:8002/; # Remember to change the port here to the corresponding project
      proxy_set_header Accept-Encoding "";
       rewrite ^/j/(.*)$ /$1 break;
      sub_filter '/login' '/j/login';
      sub_filter '/static/' '/j/static/';
      sub_filter_types *;
      sub_filter_once off;
      sub_filter '/adjuncts' '/j/adjuncts';
    }       

  I added a lot of statements here . For my convenience, let me explain :

sub_filter '/login' '/j/login';   --- We found that there is a redirection in the return value So we need to take jenkins Replace and rewrite the returned content . sub_filter       sub_filter '/adjuncts' '/j/adjuncts';  -- This is similar to .  But the versatility of firefox/chrome It's the developer tool that shows Which return failures .

Note that only in the new version nginx China supports more sub_filter.

sub_filter_types *;  -- For all request response types sub_filter Designated replacement .

sub_filter_once off; -- sub_filter It will be executed many times, not once . The effect is similar to java Medium string.replaceAll instead of replace.

proxy_set_header Accept-Encoding "";  -- The reason for setting this is to tell the back end not to do gzip Compress .  If it is gzip Compressed flow , So we can't replace it .

# Post a complete nginx.conf

        server_name test.com localhost;
        index index.html;

        root  /usr/share/nginx/ui;

        # Avoid the appearance of access 404 error

        location /api/ {
          proxy_pass http://test.com:8000; # Remember to change the port here to the corresponding project
          proxy_set_header X-Forwarded-Proto $scheme;
          proxy_set_header X-Forwarded-Port $server_port;
          proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
          proxy_set_header Upgrade $http_upgrade;
          proxy_set_header Connection "upgrade";
        }
        location /j/ {
          gzip off;
          proxy_pass http://127.0.0.1:8002/; # Remember to change the port here to the corresponding project
          proxy_set_header Accept-Encoding "";
          rewrite ^/j/(.*)$ /$1 break;
          sub_filter '/login' '/j/login';
          sub_filter '/static/' '/j/static/';
          sub_filter_types *;
          sub_filter_once off;
          sub_filter '/adjuncts' '/j/adjuncts';
        }
         location /chatbot/ {
          proxy_pass http://test.com:8001; # Remember to change the port here to the corresponding project
          proxy_set_header X-Forwarded-Proto $scheme;
          proxy_set_header X-Forwarded-Port $server_port;
          proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
          proxy_set_header Upgrade $http_upgrade;
          proxy_set_header Connection "upgrade";
        }

        location / {
          try_files $uri $uri/ @router;
          index  index.html;
        }

        location @router {
          rewrite ^.*$ /index.html last;
        }
    }
}

 

 

版权声明
本文为[KSA, the little monster by the peach blossom tree]所创,转载请带上原文链接,感谢

Scroll to Top