0

Need help editing the config below.

I have main site in / and another app (api) in the folder /api. I'm using Slim Framework, so it has accesible index.php file in /api/public. Now, I'm trying to be able to access that api using uri like https://example.com/api (hiding /public).

Current config was generated by the admin panel:

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;
}


I was trying to add additional directives inside and outside of the location / {} with alias like so:

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

No luck so far...

  • I compared what you have to this default configuration [https://www.slimframework.com/docs/v3/start/web-servers.html](url) and I think that you are missing the location block: `location / { try_files $uri /index.php$is_args$args; }` Further, if your api folder is a sub-directory (i.e., sub folder), in your location block, you have to have api after try files like this: `location / { try_files $uri /api/index.php$is_args$args; }` – Abe Dec 27 '22 at 18:58
  • yep, almost on point, thanks for the suggestion – tonyAndr Jan 06 '23 at 11:22

2 Answers2

0

So after many tries I figured it out. There is an apache behind the NGINX and the only thing I needed to make it work is to add this:

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

I gave up on the idea of using /public directory in the end to make everything simpler while testing different configs.

So my final nginx config looks like this:

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;
}
0

The puriest config i've found is:

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

Please notice /api/ before index.php

Widmo
  • 321
  • 2
  • 10