L
L
lamerboy2018-02-16 14:04:51
Nginx
lamerboy, 2018-02-16 14:04:51

How to add multiple locations in nginx?

I decided to move the project from Apache to Nginx, but there were problems with several locations, before moving the rules to htaccess were as follows:

spoiler
RewriteEngine on
RewriteBase /

RewriteRule ^js/(.*) compress/js.php?load=$1 [QSA,L]
RewriteRule ^css/(.*) compress/css.php?load=$1 [QSA,L]

RewriteCond %{REQUEST_URI} (/|\.htm|\.php|\.html|/[^.]*)$  [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) index.php [QSA,L]


After transferring to Nginx, the config is as follows:
spoiler
server {
  server_name dev.test.ru www.dev.test.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/dev.test.ru/*.conf;
  access_log /var/www/httpd-logs/dev.test.ru.access.log;
  error_log /var/www/httpd-logs/dev.test.ru.error.log notice;
  ssi on;
  set $root_path /var/www/www-root/data/www/dev.test.ru;
  root $root_path;
  
  location / {
    location ~ [^/]\.ph(p\d*|tml)$ {
    try_files /does_not_exists @php;
    }
    
    location /js {
      rewrite ^/js/(.*) /compress/js.php?load=$1 break;
    }
    
    location /css {
      rewrite ^/css/(.*) /compress/css.php?load=$1 break;
    }

    location / {
    try_files $uri $uri/ /index.php?$args; # permalinks
    }
    }
  
  listen 12.12.12.38:80;
  location @php {
    fastcgi_index index.php;
    fastcgi_param PHP_ADMIN_VALUE "sendmail_path = /usr/sbin/sendmail -t -i -f info@test.ru";
    fastcgi_pass unix:/var/www/php-fpm/www-root.sock;
    fastcgi_split_path_info ^((?U).+\.ph(?:p\d*|tml))(/?.+)$;
    try_files $uri =404;
    include fastcgi_params;
  }
}
server {
  server_name dev.test.ru www.dev.test.ru;
  ssl on;
  ssl_certificate "/var/www/httpd-cert/www-root/dev.test.ru.crt";
  ssl_certificate_key "/var/www/httpd-cert/www-root/dev.test.ru.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 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/dev.test.ru/*.conf;
  access_log /var/www/httpd-logs/dev.test.ru.access.log;
  error_log /var/www/httpd-logs/dev.test.ru.error.log notice;
  ssi on;
  set $root_path /var/www/www-root/data/www/dev.test.ru;
  root $root_path;
  
  location / {
    location ~ [^/]\.ph(p\d*|tml)$ {
    try_files /does_not_exists @php;
    }
    
    location /js {
      rewrite ^/js/(.*) /compress/js.php?load=$1 break;
    }
    
    location /css {
      rewrite ^/css/(.*) /compress/css.php?load=$1 break;
    }
    
    location / {
        try_files $uri $uri/ /index.php?$args; # permalinks
    }
    }
    
  listen 12.12.12.38:443;
  
  
  
  location @php {
    fastcgi_index index.php;
    fastcgi_param PHP_ADMIN_VALUE "sendmail_path = /usr/sbin/sendmail -t -i -f info@test.ru";
    fastcgi_pass unix:/var/www/php-fpm/www-root.sock;
    fastcgi_split_path_info ^((?U).+\.ph(?:p\d*|tml))(/?.+)$;
    try_files $uri =404;
    include fastcgi_params;
  }
}


But only the CNC of the site works, the rest of the rules output PHP as text, i.e. scripts for JS and CSS compression are not executed as scripts, but are output as text to the browser.
I have already tried different options from examples on the internet, none of them turned out to be started.
Please help me set up several locations in Nginx, here is the last option, which also does not start:
location / {
    location ~ [^/]\.ph(p\d*|tml)$ {
    try_files /does_not_exists @php;
    }
    location /js {
      rewrite ^/js/(.*) /compress/js.php?load=$1 break;
    }
    location /css {
      rewrite ^/css/(.*) /compress/css.php?load=$1 break;
    }
    location / {
        try_files $uri $uri/ /index.php?$args; # permalinks
    }
}

By the way, the config was generated by ispmanager, I only changed the location block.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
L
lamerboy, 2018-02-16
@lamerboy

The issue is resolved, the service for converting htaccess to config for Nginx helped awww.anilcetin.com

N
Nurlan, 2018-02-16
@daager

So use try_files. As an example:
rewrite ^/js/(.*) /compress/js.php?load=$1 break; => try_files $uri /compress/js.php?args;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question