D
D
DiGiTAll2018-02-09 14:11:58
Nginx
DiGiTAll, 2018-02-09 14:11:58

How to set up rewrite in Nginx?

There is this config:

server {
  server_name mysite.ru www.mysite.ru;
  charset UTF-8;
  index index.html index.php;
  disable_symlinks if_not_owner from=$root_path;
  include /etc/nginx/vhosts-includes/*.conf;
  include /etc/nginx/vhosts-resources/mysite.ru/*.conf;
  access_log /var/www/httpd-logs/mysite.ru.access.log;
  error_log /var/www/httpd-logs/mysite.ru.error.log notice;
  set $root_path /var/www/mysite/data/www/mysite.ru;
  root $root_path;
  listen 185.220.35.79:80;
  location / {
    location ~ [^/]\.ph(p\d*|tml)$ {
      try_files /does_not_exists @php;
    }
  }
  location @php {
    fastcgi_index index.php;
    fastcgi_param PHP_ADMIN_VALUE "sendmail_path = /usr/sbin/sendmail -t -i -f webmaster@mysite.ru";
    fastcgi_pass unix:/var/www/php-fpm/mysite.sock;
    fastcgi_split_path_info ^((?U).+\.ph(?:p\d*|tml))(/?.+)$;
    try_files $uri =404;
    include fastcgi_params;
  }
  return 301 https://$host:443$request_uri;
}
server {
  server_name mysite.ru www.mysite.ru;
  ssl on;
  ssl_certificate "/var/www/httpd-cert/mysite/mysite.ru_le1.crtca";
  ssl_certificate_key "/var/www/httpd-cert/mysite/mysite.ru_le1.key";
  ssl_ciphers EECDH:+AES256:-3DES:RSA+AES:!NULL:!RC4;
  ssl_prefer_server_ciphers on;
  ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
  ssl_dhparam /etc/ssl/certs/dhparam4096.pem;
  charset UTF-8;
  index index.html index.php;
  disable_symlinks if_not_owner from=$root_path;
  include /etc/nginx/vhosts-includes/*.conf;
  include /etc/nginx/vhosts-resources/mysite.ru/*.conf;
  access_log /var/www/httpd-logs/mysite.ru.access.log;
  error_log /var/www/httpd-logs/mysite.ru.error.log notice;
  set $root_path /var/www/mysite/data/www/mysite.ru;
  root $root_path;
  listen 185.220.35.79:443;
  location / {
    location ~ [^/]\.ph(p\d*|tml)$ {
      try_files /does_not_exists @php;
    }
  }
  location @php {
    fastcgi_index index.php;
    fastcgi_param PHP_ADMIN_VALUE "sendmail_path = /usr/sbin/sendmail -t -i -f webmaster@mysite.ru";
    fastcgi_pass unix:/var/www/php-fpm/mysite.sock;
    fastcgi_split_path_info ^((?U).+\.ph(?:p\d*|tml))(/?.+)$;
    try_files $uri =404;
    include fastcgi_params;
  }
  add_header Strict-Transport-Security "max-age=31536000;";
}

Task: when referring to mysite.ru/bla-bla-bla, rewrite to mysite.ru/redirect.php?get=bla-bla-bla
Something like: All other uris (for example, mysite.ru/my_script.php ) should open. At the same time, the operation of SSL and the forced redirection to the SSL scheme should not be affected. Already tired of fighting with this config. Then there is no rewrite, then it is called, but the redirect.php script itself is given in the form of plain text, instead of PHP processing. Please tell me the solution.
rewrite ^/(.+)$ /redirect.php?get=$1 break;

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
ivankomolin, 2018-02-09
@DiGiTAll

location / {
    try_files $uri $uri/ /redirect.php?get=$uri;
}

In general, try to bring configs to a more readable and simpler version.
For example, why use such a nested location? After all, there is no urgent need for it, and the config complicates it by an order of magnitude.
Here is an example of a clean config that redirects all requests to index.php if it does not find such files in the /var/www/apps/test/ folder.
And actually, if the file ends with .php it will be processed by php-fpm
server  {
        listen 80;
        server_name test.com

        root /var/www/apps/test;
        index index.php;

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

        location ~ \.php$ {
                include fastcgi_params;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                fastcgi_pass 127.0.0.1:9000;
                try_files $uri =404;
        }
}

Use this as a good example of an easy to read config.
You can also put everything related to ssl, for example, into a separate config and simply include it in the main one.
If the first server was created to redirect to the second, then why does it have all this, just like this:
server {
   listen 80;
   server_name test.com;
   return 301 https://$host$request_uri;
}
<code>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question