Различия

Показаны различия между двумя версиями страницы.

Ссылка на это сравнение

Предыдущая версия справа и слева Предыдущая версия
Следующая версия
Предыдущая версия
nginx:faq [2018/01/18 19:06] – [*Q: Надо переправить в другой location, используя ошибку] mirocownginx:faq [2025/03/10 23:59] (текущий) – [Q: Как запаролить location в Nginx] mirocow
Строка 1: Строка 1:
-{{tag>nginx faq}}+{{tag>nginx faq route alias}}
  
 ====== Nginx - Вопросы и Ответы ====== ====== Nginx - Вопросы и Ответы ======
Строка 9: Строка 9:
 ===== Вопросы-Ответы ===== ===== Вопросы-Ответы =====
  
-==== Q: Нада перенаправить с site.com на www.site.com ====+==== Q: Надо перенаправить с site.com на www.site.com ====
  
-<code>+<code nginx>
 server { server {
     listen 80;     listen 80;
Строка 39: Строка 39:
  
     * В данном случае необходимо просто проксировать запрос     * В данном случае необходимо просто проксировать запрос
-    * <code>+    * <code nginx>
 location ~* /d {   location ~* /d {  
   proxy_pass http://new-server.ru:80;    proxy_pass http://new-server.ru:80; 
Строка 81: Строка 81:
         auth_basic_user_file  /www/mysite.com/authfile;          auth_basic_user_file  /www/mysite.com/authfile;
 } }
-</code> Затем генерируем сам файл, где логин будет admin, а пароль pass <code>php -r "echo 'admin:'. crypt('pass', base64_encode('pass'));" > /www/mysite.com/authfile</code>+</code> Затем генерируем сам файл, где логин будет admin, а пароль pass <code bash>php -r "echo 'admin:'. crypt('pass', base64_encode('pass'));" > /www/mysite.com/authfile</code>
  
 ==== Q: Как перенаправить обработку скрипта в другую папку ==== ==== Q: Как перенаправить обработку скрипта в другую папку ====
Строка 100: Строка 100:
         fastcgi_split_path_info ^(.+\.php)(/.+)$;         fastcgi_split_path_info ^(.+\.php)(/.+)$;
  
- # Include the standard fastcgi_params file included with nginx+        # Include the standard fastcgi_params file included with nginx
         include fastcgi_params;         include fastcgi_params;
  
Строка 112: Строка 112:
         fastcgi_pass phpfpm;         fastcgi_pass phpfpm;
  
- #fastcgi_ignore_client_abort off; +        #fastcgi_ignore_client_abort off; 
- #try_files $uri =404;+         #try_files $uri =404;
  
         }         }
Строка 121: Строка 121:
 </code> </code>
  
 +==== Q: Как добавить / в конец ====
 +
 +
 +<code nginx>
 +rewrite ^([^.\?]*[^/])$ $1/ permanent;
 +</code>
 +
 +==== Q: Редирект на страницу ====
 +
 +<code nginx>
 +server {
 +    location = /oldpage.html {
 +        return 301 http://example.org/newpage.html;
 +    }
 +}
 +</code>
 +
 +==== Q: Распределение ресурсов между источниками CORS ====
 +
 +<code nginx>
 +location ~* .(eot|ttf|woff) { 
 +    add_header Access-Control-Allow-Origin *; 
 +}
 +</code>
 +
 +==== Q: Как завернуть location на yii appliaction ====
 +
 +<code nginx>
 +root /var/www/dev.payments-api.host.org/frontend/web;
 +
 +    location /admin/ {
 +
 +        alias /var/www/dev.payments-api.host.org/backend/web/;
 +
 +        # serve static files direct + allow friendly urls
 +        # Note: The seemingly weird syntax is due to a long-standing bug in nginx: https://trac.nginx.org/nginx/ticket/97
 +        try_files $uri $uri/ /admin//admin/index.php?$args;
 +
 +        location ~ /admin/.+\.php$ {
 +            include fastcgi_params;
 +            fastcgi_pass   127.0.0.1:9000;
 +            fastcgi_index  index.php;
 +            fastcgi_param  SCRIPT_FILENAME  $request_filename;
 +            include        fastcgi_params;
 +            fastcgi_read_timeout 300;
 +            proxy_redirect    off;
 +        }
 +
 +    } # / location
 +
 +    location @admin {
 +         rewrite ^/admin(/.*)$ /index.php?$1;
 +    }
 +
 +</code>
 +
 +==== Q: Редирект на определенный путь в URI ====
 +
 +<code nginx>
 +location /old-site {
 +    rewrite ^/old-site/(.*) http://example.org/new-site/$1 permanent;
 +}
 +</code>
 ==== Q: Количество открытых файлов и их лимиты ==== ==== Q: Количество открытых файлов и их лимиты ====
  
Строка 126: Строка 189:
 for pid in `pidof nginx`; do echo "$(< /proc/$pid/cmdline)"; egrep 'files|Limit' /proc/$pid/limits; echo "Currently open files: $(ls -1 /proc/$pid/fd | wc -l)"; echo; done for pid in `pidof nginx`; do echo "$(< /proc/$pid/cmdline)"; egrep 'files|Limit' /proc/$pid/limits; echo "Currently open files: $(ls -1 /proc/$pid/fd | wc -l)"; echo; done
 </code> </code>
 +