Rewrite для nginx + php-fpm
Rewrite для nginx + php-fpm
Перешёл с apache на nginx + php-fpm и пытаюсь сделать замену для htaccess.
Правила вроде составил
догадываюсь, что их следует записать в файл /home/admin/conf/web/nginx.conf, но не понимаю в каком месте, чтобы было правильно. Перед основным location? После него? Внутри до того, что там уже есть или после?
Подскажите, пожалуйста, что и куда.
Правила вроде составил
Code: Select all
location /tags {
rewrite ^/tags/([^/]*)/$ /tags/?tag=$1&key=tags break;
}
location / {
if (!-e $request_filename){ rewrite ^(.*)$ /index.php?q=$1 break; }
}
Code: Select all
server {
listen xxx.xxx.xxx.xxx:80;
server_name site.ru www.site.ru;
root /home/admin/web/site.ru/public_html;
index index.php index.html index.htm;
access_log /var/log/nginx/domains/site.ru.log combined;
access_log /var/log/nginx/domains/site.ru.bytes bytes;
error_log /var/log/nginx/domains/site.ru.error.log error;
location / {
location ~* ^.+\.(jpeg|jpg|png|gif|bmp|ico|svg|css|js)$ {
expires max;
}
location ~ [^/]\.php(/|$) {
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
if (!-f $document_root$fastcgi_script_name) {
return 404;
}
fastcgi_pass 127.0.0.1:9001;
fastcgi_index index.php;
include /etc/nginx/fastcgi_params;
}
}
error_page 403 /error/404.html;
error_page 404 /error/404.html;
error_page 500 502 503 504 /error/50x.html;
location /error/ {
alias /home/admin/web/site.ru/document_errors/;
}
location ~* "/\.(htaccess|htpasswd)$" {
deny all;
return 404;
}
include /etc/nginx/conf.d/phpmyadmin.inc*;
include /etc/nginx/conf.d/phppgadmin.inc*;
include /etc/nginx/conf.d/webmail.inc*;
include /home/admin/conf/web/nginx.site.ru.conf*;
}
Re: Rewrite для nginx + php-fpm
Добавлять можно после
А конструкция
не слишком удачная. С try_files будет элегантней
Code: Select all
error_log /var/log/nginx/domains/site.ru.error.log error;
Code: Select all
location / {
if (!-e $request_filename){ rewrite ^(.*)$ /index.php?q=$1 break; }
}
Code: Select all
try_files $uri $uri/ /index.php?$query_string;
Re: Rewrite для nginx + php-fpm
Спасибо! Читал, что использование if не рекомендуется, но как обойтись без него не нашёл.Stesh wrote:Добавлять можно послеКогда добавляю свои правила после error_log отдельно, то nginx отказывается запускаться:Code: Select all
error_log /var/log/nginx/domains/site.ru.error.log error;
Т.е. двух одинаковых location не должно быть. Если же пишу свои правила внутри созданного Вестой location /, то они не работают (обращение к любому адресу сайта выдаёт 500-ую ошибку).Code: Select all
Nov 26 01:24:32 site.ru systemd[1]: Starting nginx - high performance web server... Nov 26 01:24:32 site.ru nginx[23051]: nginx: [emerg] duplicate location "/" in /home/admin/conf/web/nginx.conf:17 Nov 26 01:24:32 site.ru nginx[23051]: nginx: configuration file /etc/nginx/nginx.conf test failed Nov 26 01:24:32 site.ru systemd[1]: nginx.service: control process exited, code=exited status=1 Nov 26 01:24:32 site.ru systemd[1]: Failed to start nginx - high performance web server. Nov 26 01:24:32 site.ru systemd[1]: Unit nginx.service entered failed state.
А конструкцияне слишком удачная. С try_files будет элегантнейCode: Select all
location / { if (!-e $request_filename){ rewrite ^(.*)$ /index.php?q=$1 break; } }
Code: Select all
try_files $uri $uri/ /index.php?$query_string;
Re: Rewrite для nginx + php-fpm
Показываю для примера
PS: локейшн /tags с вложенным реврайтом можно вписать в любом месте, а финт ушами с try_files добавить к корневому локейшену.
Code: Select all
error_log /var/log/блабла;
rewrite ^/((urllist|sitemap_).*\.(xml|txt)(\.gz)?)$ /vbseo_sitemap/vbseo_getsitemap.php?sitemap=$1 last;
location / {
try_files $uri $uri/ /index.php?q=$uri&$args;
}
location ~* ^.+.(js|css|png|jpg|jpeg|gif|ico|xsl)$ {
access_log off;
expires max;
}
location ~ \.php?$ {
include /home/блабла/conf/web/php.conf;
}
-
- Posts: 142
- Joined: Sun Aug 10, 2014 1:32 pm
Re: Rewrite для nginx + php-fpm
Я свой код
Перевел в nginx как
Вставляю вместо
Но почему то не работает. Кто может помочь с правильным кодом или может я делаю что-либо не так?
Code: Select all
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
Code: Select all
location / {
if (!-e $request_filename){
rewrite ^(.*)$ /index.php break;
}
}
Code: Select all
location / {
location ~* ^.+\.(jpeg|jpg|png|gif|bmp|ico|svg|css|js)$ {
expires max;
}
location ~ [^/]\.php(/|$) {
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
if (!-f $document_root$fastcgi_script_name) {
return 404;
}
fastcgi_pass 127.0.0.1:9004;
fastcgi_index index.php;
include /etc/nginx/fastcgi_params;
}
}
Re: Rewrite для nginx + php-fpm
Вместо
вот так
Т.е. добавить строчку
Больше никаких изменений в оригинальном конфиге не требуется, т.е вот ту отсебятину убрать.
Code: Select all
location / {
location ~* ^.+\.(jpeg|jpg|png|gif|bmp|ico|svg|css|js)$ {
expires max;
}
Code: Select all
location / {
try_files $uri $uri/ /index.php?$uri&$args;
location ~* ^.+\.(jpeg|jpg|png|gif|bmp|ico|svg|css|js)$ {
expires max;
}
Code: Select all
try_files $uri $uri/ /index.php?$uri&$args;