A
A
Alexander2019-04-29 18:39:35
Nginx
Alexander, 2019-04-29 18:39:35

How to rewrite Rewrite Rules for NGINX?

Good afternoon!
Switched to NGINX and there was a problem with mod_rewrite working. I have the following rules:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^([a-z]+)\.site\.ru[NC]
RewriteCond %{HTTP_HOST} !^www\.site\.ru[NC]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule . - [QSA,E=DOMAIN_LANG:%1]

RewriteRule ^download/(.*)/?$ ?do=download&id=$1
RewriteRule ^video/(.*)/(.*)/?$ ?do=view&id=$1&url=$2 [QSA,L]
RewriteRule ^error/(.*)/?$ ?do=error&error=$1
RewriteRule ^contact/?$ ?do=contact
RewriteRule ^statistics/?$ ?do=statistics
RewriteRule ^screen/(.*)/?$ ?do=screen&url=$1
RewriteRule ^novelty/?$ ?do=novelty
RewriteRule ^novelty/([0-9]*)/?$ ?do=novelty&page=$1
RewriteRule ^popular/?$ ?do=popular
RewriteRule ^popular/([0-9]*)/?$ ?do=popular&page=$1
RewriteRule ^top100/?$ ?do=top100
RewriteRule ^top100/([0-9]*)/?$ ?do=top100&page=$1 
RewriteRule ^page([0-9]*)/?$ ?do=index&page=$1
RewriteRule ^genre/([0-9]+)/([a-z0-9\-\_]+)/?$ ?do=genre&id=$1&q=$2
RewriteRule ^genre/([0-9]+)/([a-z0-9\-\_]+)/([0-9]*)/?$ ?do=genre&id=$1&q=$2&page=$3
RewriteRule ^tag/([0-9]+)/([A-z0-9\-\_]+)/?$ ?do=tag&id=$1&q=$2
RewriteRule ^tag/([0-9]+)/([A-z0-9\-\_]+)/([0-9]+)/?$ ?do=tag&id=$1&q=$2&page=$3
RewriteRule ^online/([0-9]+)/?$ ?do=online&id=$1
RewriteRule ^search/(.*)/?$ ?do=search&text=$1
RewriteRule ^search/?$ ?do=search
RewriteRule ^online_users/?$ ?do=online_users [QSA,L]

In principle, I was able to convert everything for the nginx config, except for "RewriteRule . - [QSA,E=DOMAIN_LANG:%1]".
I have a multilingual site, and the language is transmitted through a subdomain. Those. for example, when I go to kz.site.ru, I get the language version (kz) via $_SERVER['DOMAIN_LANG']. Can you please tell me how to implement this?
The config itself:
server {
  server_name site.ru *.site.ru;
  charset off;
  index index.php index.html;
  disable_symlinks if_not_owner from=$root_path;
  include /etc/nginx/vhosts-includes/*.conf;
  include /etc/nginx/vhosts-resources/site.ru/*.conf;
  access_log /var/www/httpd-logs/site.ru.access.log;
  error_log /var/www/httpd-logs/site.ru.error.log notice;
  ssi on;
  set $root_path /var/www/adult/data/www/site.ru;
  root $root_path;
  
    location / {
      if ($http_host ~* "^([a-z]+)\.site\.ru"){
        rewrite ^/download/(.*)/?$ /?do=download&id=$1;
      }
      
    	location ~ [^/]\.ph(p\d*|tml)$ {
    		try_files /does_not_exists @php;
    	}      
      
    }
    
    location /video {
      rewrite ^/video/(.*)/(.*)/?$ /?do=view&id=$1&url=$2 break;
    }
    
    location /error {
      rewrite ^/error/(.*)/?$ /?do=error&error=$1;
    }
    
    location /contact {
      rewrite ^/contact/?$ /?do=contact;
    }
    
    location /statistics {
      rewrite ^/statistics/?$ /?do=statistics;
    }
    
    location /screen {
      rewrite ^/screen/(.*)/?$ /?do=screen&url=$1;
    }
    
    location /novelty {
      rewrite ^/novelty/?$ /?do=novelty;
      rewrite ^/novelty/([0-9]*)/?$ /?do=novelty&page=$1;
    }
    
    location /popular {
      rewrite ^/popular/?$ /?do=popular;
      rewrite ^/popular/([0-9]*)/?$ /?do=popular&page=$1;
    }
    
    location /top100 {
      rewrite ^/top100/?$ /?do=top100;
      rewrite ^/top100/([0-9]*)/?$ /?do=top100&page=$1;
    }
    
    location /page {
      rewrite ^/page([0-9]*)/?$ /?do=index&page=$1;
    }
    
    location /genre {
      rewrite ^/genre/([0-9]+)/([a-z0-9\-\_]+)/?$ /?do=genre&id=$1&q=$2;
      rewrite ^/genre/([0-9]+)/([a-z0-9\-\_]+)/([0-9]*)/?$ /?do=genre&id=$1&q=$2&page=$3;
    }
    
    location /tag {
      rewrite ^/tag/([0-9]+)/([A-z0-9\-\_]+)/?$ /?do=tag&id=$1&q=$2;
      rewrite ^/tag/([0-9]+)/([A-z0-9\-\_]+)/([0-9]+)/?$ /?do=tag&id=$1&q=$2&page=$3;
    }
    
    location /online {
      rewrite ^/online/([0-9]+)/?$ /?do=online&id=$1;
    }
    
    location /search {
      rewrite ^/search/(.*)/?$ /?do=search&text=$1;
      rewrite ^/search/?$ /?do=search;
    }
    
    location /online_users {
      rewrite ^/online_users/?$ /?do=online_users break;
    }
    	
  location @php {
    fastcgi_index index.php;
    fastcgi_param PHP_ADMIN_VALUE "sendmail_path = /usr/sbin/sendmail -t -i -f webmaster@site.ru";
    fastcgi_pass unix:/var/www/php-fpm/adult.sock;
    fastcgi_split_path_info ^((?U).+\.ph(?:p\d*|tml))(/?.+)$;
    try_files $uri =404;
    include fastcgi_params;
  }
  return 301 https://$host:443$request_uri;
  listen 185.180.220.110:80;
}
server {
  server_name site.ru *.site.ru;
  ssl_certificate "/var/www/httpd-cert/adult/site.ru_le2.crt";
  ssl_certificate_key "/var/www/httpd-cert/adult/site.ru_le2.key";
  ssl_ciphers EECDH:+AES256:-3DES:RSA+AES:!NULL:!RC4;
  ssl_prefer_server_ciphers on;
  ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
  add_header Strict-Transport-Security "max-age=31536000;";
  ssl_dhparam /etc/ssl/certs/dhparam4096.pem;
  charset off;
  index index.php index.html;
  disable_symlinks if_not_owner from=$root_path;
  include /etc/nginx/vhosts-includes/*.conf;
  include /etc/nginx/vhosts-resources/site.ru/*.conf;
  access_log /var/www/httpd-logs/site.ru.access.log;
  error_log /var/www/httpd-logs/site.ru.error.log notice;
  ssi on;
  set $root_path /var/www/adult/data/www/site.ru;
  root $root_path;
  location / {
      if ($http_host ~* "^([a-z]+)\.site\.ru"){
        rewrite ^/download/(.*)/?$ /?do=download&id=$1;
      }
      
    	location ~ [^/]\.ph(p\d*|tml)$ {
    		try_files /does_not_exists @php;
    	}      
      
    }
    
    location /video {
      rewrite ^/video/(.*)/(.*)/?$ /?do=view&id=$1&url=$2 break;
    }
    
    location /error {
      rewrite ^/error/(.*)/?$ /?do=error&error=$1;
    }
    
    location /contact {
      rewrite ^/contact/?$ /?do=contact;
    }
    
    location /statistics {
      rewrite ^/statistics/?$ /?do=statistics;
    }
    
    location /screen {
      rewrite ^/screen/(.*)/?$ /?do=screen&url=$1;
    }
    
    location /novelty {
      rewrite ^/novelty/?$ /?do=novelty;
      rewrite ^/novelty/([0-9]*)/?$ /?do=novelty&page=$1;
    }
    
    location /popular {
      rewrite ^/popular/?$ /?do=popular;
      rewrite ^/popular/([0-9]*)/?$ /?do=popular&page=$1;
    }
    
    location /top100 {
      rewrite ^/top100/?$ /?do=top100;
      rewrite ^/top100/([0-9]*)/?$ /?do=top100&page=$1;
    }
    
    location /page {
      rewrite ^/page([0-9]*)/?$ /?do=index&page=$1;
    }
    
    location /genre {
      rewrite ^/genre/([0-9]+)/([a-z0-9\-\_]+)/?$ /?do=genre&id=$1&q=$2;
      rewrite ^/genre/([0-9]+)/([a-z0-9\-\_]+)/([0-9]*)/?$ /?do=genre&id=$1&q=$2&page=$3;
    }
    
    location /tag {
      rewrite ^/tag/([0-9]+)/([A-z0-9\-\_]+)/?$ /?do=tag&id=$1&q=$2;
      rewrite ^/tag/([0-9]+)/([A-z0-9\-\_]+)/([0-9]+)/?$ /?do=tag&id=$1&q=$2&page=$3;
    }
    
    location /online {
      rewrite ^/online/([0-9]+)/?$ /?do=online&id=$1;
    }
    
    location /search {
      rewrite ^/search/(.*)/?$ /?do=search&text=$1;
      rewrite ^/search/?$ /?do=search;
    }
    
    location /online_users {
      rewrite ^/online_users/?$ /?do=online_users break;
    }
  location @php {
    fastcgi_index index.php;
    fastcgi_param PHP_ADMIN_VALUE "sendmail_path = /usr/sbin/sendmail -t -i -f webmaster@site.ru";
    fastcgi_pass unix:/var/www/php-fpm/adult.sock;
    fastcgi_split_path_info ^((?U).+\.ph(?:p\d*|tml))(/?.+)$;
    try_files $uri =404;
    include fastcgi_params;
  }
  listen 185.180.220.110:443 ssl;
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
dodo512, 2019-04-29
@PR0Z0N

Create a new $domain_lang variable using map and pass it to php using fastcgi_param .

map $http_host $domain_lang {
    ~(?i)^(?>www\.|)([a-z]+)\.site\.ru$ $1;
}

server {
    location @php {
        fastcgi_param DOMAIN_LANG $domain_lang;

A
Alexander, 2019-04-29
@zkelo

htaccess converter for nginx

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question