Задать вопрос
@3v4l

Nginx + PHP-FPM не обрабатывает длинные URL с PHP-скриптами?

Добрый день!

Есть сервер с Nginx + PHP-FPM, использую такой конфиг:
example.com.conf
server {
    listen 80;
    listen [::]:80;
    server_name example.com www.example.com;
    return 301 https://$host$request_uri;

    access_log  /var/log/nginx/example.com-access.log;
    error_log   /var/log/nginx/example.com-error.log;
}

server {
    listen 443 ssl;
    listen [::]:443 ssl;
    listen 443 quic;
    listen [::]:443 quic;
    http2 on;
    http3 on;
    server_name www.example.com;

    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;

    add_header Alt-Svc 'h3=":443"; ma=86400';
    quic_retry on;
    quic_gso on;

    return 301 https://example.com$request_uri;

    access_log  /var/log/nginx/example.com-access.log;
    error_log   /var/log/nginx/example.com-error.log;
}

server {
    listen 443 ssl;
    listen [::]:443 ssl;
    listen 443 quic;
    listen [::]:443 quic;
    http2 on;
    http3 on;
    server_name example.com;

    root /var/www/example.com;
    index index.php index.html;

    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;

    add_header Alt-Svc 'h3=":443"; ma=86400';
    quic_retry on;
    quic_gso on;

    add_header X-Frame-Options "SAMEORIGIN" always;
    add_header X-XSS-Protection "1; mode=block" always;
    add_header X-Content-Type-Options "nosniff" always;
    add_header Referrer-Policy "no-referrer-when-downgrade" always;
    add_header Content-Security-Policy "default-src 'self' http: https: data: blob: 'unsafe-inline' 'unsafe-eval'" always;
    add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;

    gzip on;
    gzip_vary on;
    gzip_proxied any;
    gzip_comp_level 6;
    gzip_types text/plain text/css text/xml application/json application/javascript application/rss+xml application/atom+xml image/svg+xml;

    location / {
        try_files $uri $uri/ /index.php$is_args$args;
    }

    location ~* \.(jpg|jpeg|png|gif|ico|css|js|svg|woff|woff2|ttf|eot)$ {
        expires 365d;
        add_header Cache-Control "public, no-transform";
        access_log off;
        log_not_found off;
    }

    location ~ /\. {
        deny all;
        access_log off;
        log_not_found off;
    }

    location ~ ^/(fpm-status|fpm-ping)$ {
        access_log off;
        allow 127.0.0.1;
        deny all;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
        fastcgi_pass unix:/run/php/php8.4-fpm.sock;
    }

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+?\.php)(|/.*)$;
        fastcgi_pass unix:/run/php/php8.4-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
        fastcgi_param REQUEST_URI $request_uri;
        fastcgi_param QUERY_STRING $query_string;
        fastcgi_param SCRIPT_NAME $fastcgi_script_name;
        fastcgi_intercept_errors on;
        fastcgi_buffer_size 128k;
        fastcgi_buffers 256 16k;
        fastcgi_busy_buffers_size 256k;
        fastcgi_temp_file_write_size 256k;
        fastcgi_read_timeout 120;
    }

    access_log  /var/log/nginx/example.com-access.log;
    error_log   /var/log/nginx/example.com-error.log;
}


Проблема в том, что Nginx обрабатывает https://example.com/ajax.php, но не реагирует на https://example.com/ajax.php/url_path_test — возвращается 404.

Как правильно настроить Nginx для обработки таких URL?

Заранее спасибо за помощь.
  • Вопрос задан
  • 49 просмотров
Подписаться 1 Средний Комментировать
Решения вопроса 1
Lynn
@Lynn
nginx, js, css
location ~ \.php$ подразумевает что урл закончится на .php и никаких /url_path_test дальше быть не может.

Поменяйте на location ~ "\.php(/.+)?$"

UPD: и внутри надо поменять директиву try_files на try_files $fastcgi_script_name =404;
Ответ написан
Пригласить эксперта
Ваш ответ на вопрос

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

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