M
M
MikUrrey2022-04-04 23:30:49
Nginx
MikUrrey, 2022-04-04 23:30:49

Rewrite for nginx, how to do it right?

Here is the htaccess attached to the script:

Options All -Indexes

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l

RewriteRule ^(.+)$ index.php?altum=$1 [QSA,L]

This is how I try to rewrite the rules on nginx
server {
    listen       80;
    server_name  mysite;
    
    set $base_root /var/www/mysite;
    root   $base_root;
    
    charset utf-8;
    index  index.php;

    autoindex off;

    location / {
        if (!-e $request_filename){
            rewrite ^(.+)$ /index.php?altum=$1 break;
        }
    }

    location ~ ^/.+\.php(/|$) {

        fastcgi_pass php:9000; # proxy requests to a TCP socket
        fastcgi_index index.php;
        fastcgi_split_path_info ^(.+\.php)(.*)$;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
        fastcgi_send_timeout 30000;
        fastcgi_read_timeout 30000;
        try_files $fastcgi_script_name =404;
    }
}

those. should work like this:
/ = /index.php
/something/interesting = /index.php?=something/interesting

But the index is opened and the rest of the URLs are downloaded. What am I doing wrong in this case?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
dodo512, 2022-04-05
@MikUrrey

server {

    location / {
        try_files $uri $uri/ @rewrite;
    }

    location @rewrite {
        rewrite ^/(.*) /index.php?altum=$1;
    }
    
    location ~ \.php$ {
        ...
    }

Or
server {
    
    location ~ \.php$ {
        ...
    }

    location ~ ^/(.*) {
        try_files $uri $uri/ /index.php?altum=$1&$args;
    }

Or
map $uri $_uri {
~^/(.*)  $1;
}

server {

    location / {
        try_files $uri $uri/ /index.php?altum=$_uri&$args;
    }

    location ~ \.php$ {
        ...
    }

A
Alexander Karabanov, 2022-04-04
@karabanov

location / {
    try_files $uri $uri/ /index.php?altum=$uri;
}

location ~ \.php$ {
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_pass php:9000; # proxy requests to a TCP socket
    fastcgi_index index.php;
    fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
    include fastcgi_params;
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question