# ubuntu@ubuntu-xenial:~$ cat /etc/nginx/sites-enabled/ngx-rewrite.egolife.com.conf
upstream backend {
server 127.0.0.1:5678;
}
server {
listen 80;
server_name rewrite-normal.egolife.com;
location / {
proxy_pass http://backend;
}
}
server {
listen 80;
server_name rewrite-none.egolife.com;
rewrite ^/this/(.*) /that/$1;
rewrite ^/that/(.*) /others/$1;
rewrite ^/others/(.*) /final/$1;
location / {
proxy_pass http://backend;
}
}
server {
listen 80;
server_name rewrite-return.egolife.com;
return 200 '200, Hello, World!';
location / {
proxy_pass http://backend;
}
}
server {
listen 80;
server_name rewrite-last.egolife.com;
rewrite ^/this/(.*) /that/$1 last;
rewrite ^/that/(.*) /others/$1 last;
rewrite ^/others/(.*) /final/$1 last;
location / {
proxy_pass http://backend;
}
}
server {
listen 80;
server_name rewrite-break.egolife.com;
rewrite ^/this/(.*) /that/$1 break;
rewrite ^/that/(.*) /others/$1 break;
rewrite ^/others/(.*) /final/$1 break;
location / {
proxy_pass http://backend;
}
}
server {
listen 80;
server_name rewrite-redirect.egolife.com;
rewrite ^/this/(.*) /that/$1 redirect;
rewrite ^/that/(.*) /others/$1 redirect;
rewrite ^/others/(.*) /final/$1 redirect;
location / {
proxy_pass http://backend;
}
}
server {
listen 80;
server_name rewrite-permanent.egolife.com;
rewrite ^/this/(.*) /that/$1 permanent;
rewrite ^/that/(.*) /others/$1 permanent;
rewrite ^/others/(.*) /final/$1 permanent;
location / {
proxy_pass http://backend;
}
}
server {
listen 80;
server_name rewrite-args.egolife.com;
# discard all args
rewrite ^/this/(.*) /that/$1?;
location / {
proxy_pass http://backend;
}
}
server {
listen 80;
server_name rewrite-combination1.egolife.com;
rewrite ^/this/(.*) /that/$1;
rewrite ^/that/(.*) /others/$1;
rewrite ^/others/(.*) /location/$1;
location ^~ /location {
rewrite ^/location /next;
rewrite ^/next /final;
proxy_pass http://backend;
}
location / {
proxy_pass http://backend;
}
}
server {
listen 80;
server_name rewrite-combination2.egolife.com;
rewrite ^/this/(.*) /that/$1;
rewrite ^/that/(.*) /others/$1;
rewrite ^/others/(.*) /location/$1;
rewrite ^/strange/(.*) /dead/$1;
location ^~ /location {
rewrite ^/location/(.*) /strange/$1 last;
proxy_pass http://backend;
}
location /strange {
proxy_pass http://backend;
}
location / {
proxy_pass http://backend;
}
}
server {
listen 80;
server_name rewrite-location.egolife.com;
# return
location ^~ /return {
return 200 '200, Hello, World!';
return 500 '500, Hello, World!';
}
# none
location ^~ /flags/none {
rewrite ^/flags /none;
rewrite ^/none /final;
proxy_pass http://backend;
}
# rewrite last 1
location ^~ /flags/last1 {
rewrite ^/flags /last last;
rewrite ^/last /final last;
proxy_pass http://backend;
}
# rewrite last 2
location ^~ /flags/last2 {
rewrite ^/flags/last2(.*) /flags/last1$1 last;
proxy_pass http://backend;
}
# rewrite break
location ^~ /flags/break {
rewrite ^/flags /break break;
rewrite ^/break /final break;
proxy_pass http://backend;
}
# rewrite redirect
location ^~ /flags/redirect {
rewrite ^/flags /final redirect;
proxy_pass http://backend;
}
# rewrite redirect
location ^~ /flags/permanent {
rewrite ^/flags /final permanent;
proxy_pass http://backend;
}
# rewrite to full uri with current virtual server
location ^~ /rewrite/current_virtual_server {
rewrite ^/rewrite http://rewrite.egolife.com/dead;
proxy_pass http://backend;
}
# rewrite to full uri with another virtual server
location ^~ /rewrite/another_virtual_server {
rewrite ^/rewrite http://location.egolife.com/caret-tilde;
proxy_pass http://backend;
}
# rewrite to full uri with external server
location ^~ /rewrite/external_server {
rewrite ^/rewrite https://dylanninin.com;
proxy_pass http://backend;
}
# rewrite max 10 times
location ^~ /rewrite/max_10 {
rewrite ^/(.*)/max_10(.*) /$1/max_10$2 last;
proxy_pass http://backend;
}
location / {
proxy_pass http://backend;
}
}
ubuntu@ubuntu-xenial:~$ cat /etc/nginx/nginx.conf
...
# 1. specify error log level to notice
error_log logs/error.log notice;
...
http {
...
# 2. open rewrite log
rewrite_log on;
...
}
# terminal 1
http :80/this/path Host:rewrite-none.egolife.com
# terminal 2
sudo tail -f /var/log/nginx/*.log
==> /var/log/nginx/access.log <==
127.0.0.1 - - [15/May/2018:19:54:50 +0800] "GET /this/path HTTP/1.1" 200 5 154 0.001 "-" "HTTPie/0.9.2"
==> /var/log/nginx/error.log <==
2018/05/15 19:54:50 [notice] 24417#24417: *169 "^/this/(.*)" matches "/this/path", client: 127.0.0.1, server: rewrite-none.egolife.com, request: "GET /this/path HTTP/1.1", host: "rewrite-none.egolife.com"
2018/05/15 19:54:50 [notice] 24417#24417: *169 rewritten data: "/that/path", args: "", client: 127.0.0.1, server: rewrite-none.egolife.com, request: "GET /this/path HTTP/1.1", host: "rewrite-none.egolife.com"
2018/05/15 19:54:50 [notice] 24417#24417: *169 "^/that/(.*)" matches "/that/path", client: 127.0.0.1, server: rewrite-none.egolife.com, request: "GET /this/path HTTP/1.1", host: "rewrite-none.egolife.com"
2018/05/15 19:54:50 [notice] 24417#24417: *169 rewritten data: "/others/path", args: "", client: 127.0.0.1, server: rewrite-none.egolife.com, request: "GET /this/path HTTP/1.1", host: "rewrite-none.egolife.com"
2018/05/15 19:54:50 [notice] 24417#24417: *169 "^/others/(.*)" matches "/others/path", client: 127.0.0.1, server: rewrite-none.egolife.com, request: "GET /this/path HTTP/1.1", host: "rewrite-none.egolife.com"
2018/05/15 19:54:50 [notice] 24417#24417: *169 rewritten data: "/final/path", args: "", client: 127.0.0.1, server: rewrite-none.egolife.com, request: "GET /this/path HTTP/1.1", host: "rewrite-none.egolife.com"