使用 Slim Framework 为子目录应用程序配置 NGINX

使用 Slim Framework 为子目录应用程序配置 NGINX

需要帮助编辑下面的配置。

我在 / 中有一个主站点,在 /api 文件夹中有一个应用程序 (api)。我使用的是 Slim Framework,因此它在 /api/public 中有一个可访问的 index.php 文件。现在,我尝试使用类似 uri 的方式访问该 apihttps://example.com/api(隐藏/公开)。

当前配置由管理面板生成:

server {
    server_name example.com www.example.com;
    charset off;
    index index.php index.html;
    disable_symlinks if_not_owner from=$root_path;
    include /etc/nginx/vhosts-includes/*.conf;
    include /etc/nginx/vhosts-resources/example.com/*.conf;
    access_log /var/www/httpd-logs/example.com.access.log;
    error_log /var/www/httpd-logs/example.com.error.log notice;
    ssi on;
    set $root_path /var/www/example.com/data/www/example.com;
    root $root_path;

    location / {
        location ~ [^/]\.ph(p\d*|tml)$ {
            try_files /does_not_exists @fallback;
        }
        location ~* ^.+\.(jpg|jpeg|gif|png|svg|js|css|mp3|ogg|mpe?g|avi|zip|gz|bz2?|rar|swf)$ {
            try_files $uri $uri/ @fallback;
            expires max;
        }
        location / {
            try_files /does_not_exists @fallback;
        }

    }

    location @fallback {
        proxy_pass http://127.0.0.1:8081;
        proxy_redirect http://127.0.0.1:8081 /;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header X-Forwarded-Port $server_port;
        access_log off;
    }
    gzip on;
    gzip_comp_level 5;
    gzip_disable "msie6";
    gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript application/javascript;
    return 301 https://$host:443$request_uri;
    listen 185.200.200.228:80;
}
server {
    server_name example.com www.example.com;
    ssl_certificate "/var/www/httpd-cert/example.com/example.com_le1.crtca";
    ssl_certificate_key "/var/www/httpd-cert/example.com/example.com_le1.key";
    ssl_ciphers EECDH:+AES256:-3DES:RSA+AES:!NULL:!RC4;
    ssl_prefer_server_ciphers on;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3;
    add_header Strict-Transport-Security "max-age=31536000;";
    ssl_dhparam /etc/ssl/certs/dhparam4096.pem;
    charset off;
    index index.php index.html;
    disable_symlinks if_not_owner from=$root_path;
    include /etc/nginx/vhosts-includes/*.conf;
    include /etc/nginx/vhosts-resources/example.com/*.conf;
    access_log /var/www/httpd-logs/example.com.access.log;
    error_log /var/www/httpd-logs/example.com.error.log notice;
    ssi on;
    set $root_path /var/www/example.com/data/www/example.com;
    root $root_path;
    gzip on;
    gzip_comp_level 5;
    gzip_disable "msie6";
    gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript application/javascript;

    location / {

        location ~ [^/]\.ph(p\d*|tml)$ {
            try_files /does_not_exists @fallback;
        }
        location ~* ^.+\.(jpg|jpeg|gif|png|svg|js|css|mp3|ogg|mpe?g|avi|zip|gz|bz2?|rar|swf)$ {
            expires max;
            try_files $uri $uri/ @fallback;
        }
        location ~ \.php$ {
            try_files /does_not_exists @fallback;
        }

    }


    location @fallback {
        proxy_pass http://127.0.0.1:8081;
        proxy_redirect http://127.0.0.1:8081 /;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header X-Forwarded-Port $server_port;
        access_log off;
    }
    listen 185.200.200.228:443 ssl http2;
}


我试图在地点 / {}别名如下:

location /api {
    alias /var/www/example.com/data/www/example.com/api/public;
}

至今还没有运气...

答案1

经过多次尝试,我终于找到了答案。NGINX 背后有一个 Apache,我只需要添加以下内容即可使其正常工作:

location /api {
    try_files $uri $uri/api @fallback;
}

我最终放弃了使用 /public 目录的想法,以便在测试不同的配置时让一切变得更简单。

所以我的最终 nginx 配置如下所示:

server {
    server_name example.com www.example.com;
    ssl_certificate "/var/www/httpd-cert/example.com/example.com_le1.crtca";
    ssl_certificate_key "/var/www/httpd-cert/example.com/example.com_le1.key";
    ssl_ciphers EECDH:+AES256:-3DES:RSA+AES:!NULL:!RC4;
    ssl_prefer_server_ciphers on;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3;
    add_header Strict-Transport-Security "max-age=31536000;";
    ssl_dhparam /etc/ssl/certs/dhparam4096.pem;
    charset off;
    index index.php index.html;
    disable_symlinks if_not_owner from=$root_path;
    include /etc/nginx/vhosts-includes/*.conf;
    include /etc/nginx/vhosts-resources/example.com/*.conf;
    access_log /var/www/httpd-logs/example.com.access.log;
    error_log /var/www/httpd-logs/example.com.error.log notice;
    ssi on;
    set $root_path /var/www/example.com/data/www/example.com;
    root $root_path;
    gzip on;
    gzip_comp_level 5;
    gzip_disable "msie6";
    gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript application/javascript;

    location / {

        location ~ [^/]\.ph(p\d*|tml)$ {
            try_files /does_not_exists @fallback;
        }
        location ~* ^.+\.(jpg|jpeg|gif|png|svg|js|css|mp3|ogg|mpe?g|avi|zip|gz|bz2?|rar|swf)$ {
            expires max;
            try_files $uri $uri/ @fallback;
        }
        location ~ \.php$ {
            try_files /does_not_exists @fallback;
        }

    }

    location /api {
        try_files $uri $uri/api @fallback;
    }

    location @fallback {
        proxy_pass http://127.0.0.1:8081;
        proxy_redirect http://127.0.0.1:8081 /;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header X-Forwarded-Port $server_port;
        access_log off;
    }
    listen 185.200.200.228:443 ssl http2;
}

答案2

我发现的最纯粹的配置是:

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

请注意/api/在 index.php 之前

相关内容