Задать вопрос

Как переделать Rewrite Rules для NGINX?

Добрый день!
Перешел на NGINX и возникла проблема с работой mod_rewrite. У меня были прописаны следующие правила:
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]

В принципе все смог переконвертировать для конфига nginx, за исключением "RewriteRule . - [QSA,E=DOMAIN_LANG:%1]".
У меня мультиязычный сайт, и язык передается через поддомен. Т.е. например при переходе на kz.site.ru, я получаю версию языка (kz) через $_SERVER['DOMAIN_LANG']. Подскажите пожалуйста, как это реализовать?
Сам конфиг:
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;
}
  • Вопрос задан
  • 1342 просмотра
Подписаться 2 Простой Комментировать
Решения вопроса 1
@dodo512
C помощью map cоздать новую переменную $domain_lang и передать php c помощью fastcgi_param.
map $http_host $domain_lang {
    ~(?i)^(?>www\.|)([a-z]+)\.site\.ru$ $1;
}

server {
    location @php {
        fastcgi_param DOMAIN_LANG $domain_lang;
Ответ написан
Пригласить эксперта
Ответы на вопрос 1
Комментировать
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Похожие вопросы