Detail nginx rewrite and location according to the url parameter
- 2020-05-12 06:48:22
- OfStack
The recent project involves the migration of old projects, and some configuration needs to be done on nginx. Therefore, after a simple study, a good memory is not as good as a rotten pen, so write it down first.
rewrite
First check if nginx supports rewrite:
./nginx -V
It is not supported that pcre is missing when installing nginx, and nginx needs to be reinstalled:
# The installation 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
# The installation 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 the nginx
./nginx
# restart nginx
./nginx � s reload
Example:
For example, the existing nginx configuration is as follows:
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 500 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_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log logs/access_log.log log;
# Set the view Nginx Address of status
location /NginxStatus {
#stub_status on;
access_log on;
auth_basic "NginxStatus";
#auth_basic_user_file conf/htpasswd;
}
}
}
Now you need to do the following redirection:
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
It 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 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;
}
The rewrite configuration here mainly explains the following points:
rewrite usage: rewrite regular substitution flag bit The order of line 1 configuration and line 2 configuration cannot be reversed, because nginx will rewrite from top to bottom (break does not work here); (& # 63; !). Means ignore case matching (the Internet says ~*, but it doesn't seem to work, my nginx version is 1.0.12); 1,1,2 represents the part to which the previous regular expression matches; rewrite can be in server or location, nginx will first execute rewrite in server, then location, which means location will be rewritten url, then rewrite in location, and finally nginx will take the result and execute location.
According to the url parameter location
In the actual development, it is often used to route from different request handlers according to the request parameters. Some nginx plug-ins are needed according to the request parameters of POST. Here is a brief introduction of how to route according to the parameters of GET.
The same configuration file as above. For example, we want to visit http: / / 192.168.71.51:6061 / do1 aspx & # 63; t=1212&c=uplog when the parameter c in url is config or uplog (case ignored) we route to somewhere else:
First add 1 upstream, for example:
...
upstream other {
server 192.168.71.41:2210;
}
...
Then add the following judgment in location:
...
location / {
if ( $query_string ~* ^(.*)c=config\b|uplog\b(.*)$ ){
proxy_pass http://other;
}
...
The key is the line marked in red, $query_string represents the url parameter, followed by the standard regular matching. It should be noted that if in nginx has many restrictions and strict syntax, please refer to the above document for details.
Very simple but very practical configuration, hope to help the students who are looking for this information.