Skip to content

Instantly share code, notes, and snippets.

@iron-viper
Forked from un1ko85/nginx.conf
Created May 12, 2024 12:29
Show Gist options
  • Save iron-viper/c476ee1a4133dacaacd03241d3712ab5 to your computer and use it in GitHub Desktop.
Save iron-viper/c476ee1a4133dacaacd03241d3712ab5 to your computer and use it in GitHub Desktop.
Rewrite URI with nginx and php-fpm. I have faced the problem that REQUEST_URI parameter is not changed on nginx rewrite rule. After some research I have found solution with replacing $request_uri variable.
server {
listen 80;
server_name site.dev;
index index.php;
root /Users/balkon_smoke/Sites/site.dev/web;
error_log /Users/balkon_smoke/Sites/site.dev/logs/error.log;
access_log /Users/balkon_smoke/Sites/site.dev/logs/access.log;
location / {
try_files $uri $uri/ /index.php;
}
# save and check current $request_uri
set $request_url $request_uri;
if ($request_uri ~ ^/categories/view/(.*)$ ) {
# change it if condition is true
set $request_url /?category=$1;
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi_params;
# replace it with modified
fastcgi_param REQUEST_URI $request_url;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param HTTPS off;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment