NGINX 获取子文件夹中的应用程序以使用重定向

NGINX 获取子文件夹中的应用程序以使用重定向

我已经安装了毛蒂克在子文件夹中,例如:example.com/m
大多数 Mautic 都可以很好地与我的配置(管理面板、使用 Mautic 等)配合使用,但对于我的子文件夹中的某些重定向,我得到了404

example.com/m 200
example.com/m/form/9 200
example.com/m/form/generate.js?id=9 404

(/form 不存在 - 它应该由 Mautic 动态重定向/生成)

文件系统上的所有文件都属于www-data。

我的主机的 nginx 配置如下:

server {
    server_name www.example.com;
    rewrite_log on;

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

    access_log /var/log/nginx/example.com_access.log;
    error_log /var/log/nginx/example.com_error.log notice;

    #######################################
    ##  Start Mautic Specific config #####
    #######################################

    location /m {
        # Working despite forms:
        try_files $uri $uri/ /m/?q=$uri&$args;
    }

    #######################################
    ##  End Mautic Specific config #####
    #######################################

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

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php/php7.3-fpm.sock;
    }

    # A long browser cache lifetime can speed up repeat visits to your page
    location ~* \.(jpg|jpeg|gif|png|webp|svg|woff|woff2|ttf|css|js|ico|xml)$ {
       access_log        off;
       log_not_found     off;
       expires           360d;
    }

    # disable access to hidden files
    location ~ /\.ht {
      access_log off;
      log_not_found off;
      deny all;
    }

    listen [::]:443 ssl; # managed by Certbot
    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/stats.example.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/stats.example.com/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}

server {
    listen 443 ssl;
    server_name example.com;
    listen [::]:443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/stats.example.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/stats.example.com/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
    return 301 https://www.example.com$request_uri;

}

server {
    if ($host = example.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


    if ($host = www.example.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


    server_name example.com www.example.com;
    listen 80;
    listen [::]:80;
    return 301 https://$host$request_uri;
    #return 404; # managed by Certbot

}

我究竟做错了什么?

答案1

您希望以 开头的 URI/m由该块处理location /m

但是,正则表达式位置优先,因此 URI/m/form/generate.js实际上由与所有以 结尾的 URI 匹配的另一个位置处理.js。请参阅这个文件

有一个^~运算符强制前缀位置优先,但在您的情况下不起作用,因为应用程序是 PHP,并且您需要以 结尾的 URI.php才能由该块处理location ~ \.php$

因此,我建议您将location导致问题的原因嵌套在location /块内。

例如:

location /m {
    try_files $uri $uri/ /m/?q=$uri&$args;
}

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

    location ~* \.(jpg|jpeg|gif|png|webp|svg|woff|woff2|ttf|css|js|ico|xml)$ {
        access_log        off;
        log_not_found     off;
        expires           360d;
    }
}

location ~ \.php$ {
    ...
}

location ~ /\.ht {
    ...
}

相关内容