Detailed Nginx rewrite and location_nginx based on URL parameters

Source: Internet
Author: User
Tags log log regular expression first row

Recent projects involving old and old project migration, need to do some configuration on the Nginx, so a simple study, good memory is not as bad as written, first recorded down.

Rewrite

First check to see if the nginx supports rewrite:

./nginx-v

Does not support the installation nginx the absence of pcre, the need to reinstall Nginx:

#安装pcre
wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.34.tar.gz
TAR-ZXVF pcre-8.34.tar.gz
cd pcre-8.34
./configure make make
install
#安装nginx
CD nginx-1.0.12
./configure--conf-path=/usr/local/nginx/conf/nginx.conf \
--pid-path=/usr/local/nginx/nginx.pid \
--with-http_ssl_module \
--with-pcre=/usr/local/src/pcre-8.34 \ Make make
install
# Start Nginx
./nginx
#重启nginx
./nginx–s Reload

Example:

For example, the following Nginx configuration is available:

Worker_processes 24;

#worker_cpu_affinity 0000000000000001;

Worker_rlimit_nofile 65535;

Error_log Logs/error.log Crit;

PID Logs/nginx.pid; 
  events {use Epoll;
Worker_connections 2048000;
  } http {include mime.types;
  Default_type Application/octet-stream;

  CharSet Utf-8;
  Sendfile on;
  Tcp_nopush on;
  Tcp_nodelay on;
  Keepalive_timeout 60; 
  Client_max_body_size 10m; 

  Client_body_buffer_size 128k;

  Upstream log {server 192.168.80.147:8338;
    } server {Listen 6061;

    server_name 192.168.71.51; 
      Location/{Proxy_pass http://log; 
      Proxy_redirect off; 
      Proxy_set_header Host $host; 
      Proxy_set_header remote_addr $remote _addr; 
      Proxy_set_header X-real-ip $remote _addr; 
     
      Proxy_set_header x-forwarded-for $proxy _add_x_forwarded_for; 
      Proxy_connect_timeout 90; 
      Proxy_send_timeout 90; 
      Proxy_read_timeout 90; proxy_buffer_sIze 4k; 
      Proxy_buffers 4 32k; 
      Proxy_busy_buffers_size 64k;
    Proxy_temp_file_write_size 64k;
    } error_page 502 503 504/50x.html;
    Location =/50x.html {root html; } log_format log ' $remote _addr-$remote _user [$time _local] "$request" "$status $body _bytes_sent" $http _re

    "Ferer" "$http _user_agent" "$http _x_forwarded_for";

    
    Access_log Logs/access_log.log log;  
    #设定查看Nginx状态的地址 Location/nginxstatus {#stub_status on;  
    Access_log on;  
    Auth_basic "Nginxstatus";  
    #auth_basic_user_file conf/htpasswd;

 }
  }
}

The following redirects are now required:

192.168.71.51/log.aspx–> 192.168.80.147:8338/log

192.168.71.51/do.aspx–> 192.168.80.147:8338/do

192.168.71.51/uplog.aspx–> 192.168.80.147:8338/log

Can be configured as follows:

server {
    listen    6061;
    server_name 192.168.71.51;

  Rewrite ^ (. *) (? i) uplog.aspx (. *) $ $1log$2 break;
  Rewrite ^ (. *) (? i) log.aspx (. *) $ $1log$2 break;
  Rewrite ^ (. *) (? i) do.aspx (. *) $ $1do$2 break;
  

    Location/{ 
      proxy_pass         http://log; 
      Proxy_redirect off       ; 
      Proxy_set_header      Host $host; 
      Proxy_set_header      remote_addr $remote _addr; 
      Proxy_set_header  x-real-ip $remote _addr; 
      Proxy_set_header      x-forwarded-for $proxy _add_x_forwarded_for; 
     
      Proxy_connect_timeout    ; 
      Proxy_send_timeout     ; 
      Proxy_read_timeout     ; 
      Proxy_buffer_size      4k; 
      Proxy_buffers        4 32k; 
      Proxy_busy_buffers_size   64k; 
      Proxy_temp_file_write_size 64k;
    }

The rewrite configuration here mainly describes the following points:

    • Rewrite usage: Rewrite regular replacement flag bit
    • The first row configuration and the second row configuration order cannot be reversed, because the nginx will be rewrite from the top down (the break does not work here);
    • (?! to ignore case matching (online is ~*, but it doesn't seem to work, my nginx version is 1.0.12);
    • 1,1,2 represents the part of the preceding regular expression to match;
    • Rewrite can be in the server or in the location, Nginx will first execute the rewrite in the server before executing location, meaning that the location is the rewritten URL, After that, the rewrite in location will be executed, and Nginx will take the result to perform the remaining location.

Location based on URL parameters

In the actual development, according to the request parameters are often routed to the different request processor, according to the POST request parameters need some Nginx plug-ins, here is a brief introduction of how to use the Get parameters to route.

Or the configuration file above. For example we want to access http://192.168.71.51:6061/do1.aspx?t=1212&c=uplog when parameter C in the URL is config or uplog (ignore case) we route to another place:

First, add a upstream, such as:


..... Upstream other { 
  server 192.168.71.41:2210;

   }
......

Then add the following judgment in the location:


..... Location/{ 

    if ($query _string ~* ^ (. *) c=config\b|uplog\b (. *) $) {
     proxy_pass         http://other; 
    }
......

The key is the red line, $query _string represents the URL parameters, followed by the standard regular match, the need to note that nginx if there are many restrictions, the syntax is very harsh, see the above document.

Very simple but very practical configuration, hoping to help the students are looking for this information.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.