Welcome! Log In Create A New Profile

Advanced

Nginx rewrite not working as expected

Posted by businessidhu 
Nginx rewrite not working as expected
September 16, 2019 08:41AM
Here directories structure
http://localhost -> /usr/local/nginx/web/webroot
http://localhost/sess8283737/wordpress -> /usr/local/nginx/wordpress
http://localhost/sess244737/phpmyadmin-> /usr/local/nginx/phpmyadmin


I have below block, it works fine with index.html, but does not work when actual php files
when user visits http://localhost/sess8283737/wordpress and I want nginx to load files from /usr/local/nginx/wordpress
and set sess8283737 as variable

server {
listen 80;
server_name localhost;
root /usr/local/nginx/web/webroot;

location / {
root /usr/local/nginx;
if ($uri ~ "^/sess([a-z0-9]+)/(wordpress)") {
rewrite ^/sess([a-z0-9]+)/wordpress /wordpress/$2;
}

if ($uri ~ "^/sess([a-z0-9]+)/(phpmyadmin)") {
rewrite ^/sess([a-z0-9]+)/phpmyadmin /phpmyadmin/$2;
}

location \.php$ {
if ($uri ~ "^/sess([a-z0-9]+)/(wordpress)") {
rewrite ^/sess([a-z0-9]+)/wordpress /wordpress/$2;
}

if ($uri ~ "^/sess([a-z0-9]+)/(phpmyadmin)") {
rewrite ^/sess([a-z0-9]+)/phpmyadmin /phpmyadmin/$2;
}

include fastcgi.conf;
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
}

}
Re: Nginx rewrite not working as expected
September 16, 2019 12:44PM
Below block working file http://192.168.112.132/sess454/wordpress
If index.php file just have echo "index";

If there actual wordpress files then it redirect to http://192.168.112.132/wordpress
But I want to keep session token in url http://192.168.112.132/sess454/wordpress


location ~ ^/sess([a-z0-9]+)/(wordpress)(.*)?\.php$ {
root /usr/local/nginx;
include fastcgi.conf;
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;

if ($uri ~ "^/sess([a-z0-9]+)/(wordpress)") {
rewrite ^/sess([a-z0-9]+)/wordpress /wordpress/$2;
}


if ($uri ~ "^/sess([a-z0-9]+)/(phpmyadmin)") {
rewrite ^/sess([a-z0-9]+)/phpmyadmin /phpmyadmin/$2;
}
}
Re: Nginx rewrite not working as expected
September 16, 2019 06:27PM
It's a bit hard to understand what you are really trying to do. I am guessing it may be something along the lines of this:

map $realURI $handler {
"~*/sess.*[.]php$" "_php";
"~*/sess.*[.](jpg,gif,png,svg)$" "_images";
"~*/sess.*$" "_static";
default "_session";
}

location ~ "^/(?<sessionID>sess[a-z0-9]+)(?<realURI>.*)$" {
rewrite "^.*$" "/$handler/$realURI";
}

location /_php {
internal;
# do your fastcgi stuff
}

location /_images {
internal;
# deliver from your image repository
}

location /_static {
internal;
# deliver your static content
}

location /_session {
internal;
# create a session and redirect further, e.g., via js_content
}

The idea is to have a classification of the URI with the map directive. Then you do internal redirects to the actual (internal) location handling the case properly. This way, you may set entirely different headers, proxy parameters, etc. for each handler. You may even handle cases without the session id by directing such requests to a location with a Javascript handler, that creates a session according to custom logic and then redirects to the proper location handling the content.

--j.
Re: Nginx rewrite not working as expected
September 17, 2019 04:24AM
I have added whole conf below and URI is http://192.168.112.132/sess1234/wordpress
In log I'm getting "/a$handler/b$realURI"; why variable are empty not sure may be regex not match
/usr/local/nginx1/a_session/b/wordpress > GET /sess1234/wordpress HTTP/1.1



```
user root;
worker_processes 1;
error_log logs/error_debug.log debug;

events {
worker_connections 1024;
}

http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;

client_max_body_size 2000M;
log_format scripts '$document_root$fastcgi_script_name > $request';
access_log logs/error.log scripts;

map $realURI $handler {
"~*/sess.*[.]php$" "_php";
"~*/sess.*[.](jpg,gif,png,svg)$" "_images";
"~*/sess.*$" "_static";
default "_session";
}

server {
listen 80;
server_name localhost;
root /usr/local/nginx1;
index index.php index.html;

location ~ "^/(?<sessionID>sess[a-z0-9]+)(?<realURI>.*)$" {
rewrite "^.*$" "/a$handler/b$realURI";
}

location /_php {
internal;
include fastcgi.conf;
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
}



error_page 500 502 503 504 /50x.html;

location = /50x.html {



root html;

}

}

}
```
Sorry, only registered users may post in this forum.

Click here to login

Online Users

Guests: 87
Record Number of Users: 6 on February 13, 2018
Record Number of Guests: 421 on December 02, 2018
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready