Slim settings on nginx


#1

I am very new to nginx. So I managed to setup with a lot of googling ready.
I have one pure php app running on this folder /var/www/html/app1

Next I want to build api links. So first I did this

composer create-project slim/slim-skeleton

I want to use the slim framework.So the slim-skeleton folder I renamed it as apitest. Next I adjusted my nginx config file its as below. Below I added this configuration

location /apitest {
alias /var/www/html/apitest1/public;
try_files $uri $uri/ /index.php$is_args$args;

location ~ .php$ {
include fastcgi.conf;
fastcgi_param SCRIPT_FILENAME $request_filename;
fastcgi_pass 127.0.0.1:9000;
}
}
but I keep getting 404 not found.


#2

Try root instead of alias


#3

Hi Victor,
I have tried is the same still.


#4
server {
    listen *:80;
    server_name server.cam ;
    root  /var/www/html/apitest1/public;

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

    location ~ .php$ {
         include fastcgi.conf;
         fastcgi_index index.php;
         fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
         fastcgi_intercept_errors on;         
         fastcgi_pass 127.0.0.1:9000;
    }
}

Attention to php fast_cgi params. I’m using it on my servers.

Helps too if you explain path of your files and nginx error log output.

Regards,


#5

Hi Victor,
Actually in my /var/www/html another different folder app for pure php application. But I just went ahead with you suggestion.
I created a new folder for the slim/slim-skeleton as apiv1. Below is the error log I got

2018/06/05 01:29:30 [error] 12246#0: 9 open() “/var/www/html/apiv1/public/50x.html” failed (2: No such file or directory), client: ..., server: _, request: “GET /apiv1/ HTTP/1.1”, upstream: “fastcgi://127.0.0.1:9000”, host: "...*"

I have check /var/www/html/apiv1/public this folder exist and even there index.php in it too.


#6

NGINX is telling you that not found 50x.html. This means error, maybe in php.

But, you could try:
If you have in your server area:

root /var/www/html/

Then you need to configure location to explain where index.php is:

location ~ ^/apiv1 {
    try_files $uri $uri/ /apiv1/public/index.php$is_args$args;
}

#7

Here is my current full conf. Yet I am getting the same type of errors
2018/06/05 01:50:18 [error] 12287#0: 9 open() “/var/www/html/50x.html” failed (2: No such file or directory), client: ******, server: _, request: “GET /apiv1/token HTTP/1.1”, upstream: “fastcgi://127.0.0.1:9000”, host: "*****"

But if just to till the /apiv1 then I get 503 forbidden.
2018/06/05 01:51:19 [error] 12287#0: 13 directory index of “/var/www/html/apiv1/” is forbidden, client: ******, server: _, request: “GET /apiv1/ HTTP/1.1”, host: "*****"

http {
log_format main '$remote_addr - $remote_user [$time_local] “$request” ’
'$status $body_bytes_sent “$http_referer” ’
‘"$http_user_agent" “$http_x_forwarded_for”’;
server_tokens off;
access_log /var/log/nginx/access.log main;

sendfile            on;
tcp_nopush          on;
tcp_nodelay         on;
keepalive_timeout   65;
types_hash_max_size 2048;

include             /etc/nginx/mime.types;
default_type        application/octet-stream;

# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/blockuseragents.rules;
limit_conn_zone $binary_remote_addr zone=addr:5m;
server {
    listen       80 default_server;
    listen       [::]:80 default_server;
    server_name  _;
    #root         /usr/share/nginx/html;
    root /var/www/html/;

    # Load configuration files for the default server block.
    include /etc/nginx/default.d/*.conf;

    
    location ~ ^/apiv1 {
      try_files $uri $uri/ /apiv1/public/index.php$is_args$args;
    }        
     error_page 404 /404.html;
        location = /40x.html {
    }

    error_page 500 502 503 504 /50x.html;
        location = /50x.html {
    }
    if ($request_method !~ ^(GET|HEAD|POST)$) {
        return 444;
    }
    limit_conn addr 1;
   
}

#8

I am just wondering could it be due to this file # Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;?
The content of it is the php.conf.

pass the PHP scripts to FastCGI server

See conf.d/php-fpm.conf for socket configuration

index index.php index.html index.htm;

location ~ .php$ {
try_files $uri =404;
fastcgi_intercept_errors on;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass php-fpm;
}


#9

Remove this and you will see the correct error in error_log.


#10

Hi Victor,
I did like you suggested. Now when I got just to http://myip/apiv1 I get 403 forbidden. Next say I type http://myip/apiv1/token I get the index.php from /var/www/html.

 listen       80 default_server;
    listen       [::]:80 default_server;
    server_name  _;
    
    #root         /usr/share/nginx/html;
    root /var/www/html/;
    index index.php index.html home.html;
    # Load configuration files for the default server block.
    #include /etc/nginx/default.d/*.conf;
    include /etc/nginx/default.d/*.conf;
    
    location ~ ^/apiv1 {
    try_files $uri $uri/ /index.php$is_args$args;
    }

    location ~ .php$ {
     include fastcgi.conf;
     fastcgi_index index.php;
     fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
     fastcgi_intercept_errors on;         
     fastcgi_pass 127.0.0.1:9000;
    } 
    #error_page 404 /404.html;
    #    location = /40x.html {
    #}

    #error_page 500 502 503 504 /50x.html;
    #    location = /50x.html {
    #}
    if ($request_method !~ ^(GET|HEAD|POST)$) {
        return 444;
    }
    limit_conn addr 1;
    
    #location ~ \.php {
    #try_files $uri =404;
    #fastcgi_split_path_info ^(.+\.php)(/.+)$;
    #include fastcgi_params;
    #fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    #fastcgi_param SCRIPT_NAME $fastcgi_script_name;
    #fastcgi_index index.php;
    #fastcgi_pass 127.0.0.1:9000;
    #}
    
}

#11

This:

is:

location ~ ^/apiv1 {
    try_files $uri $uri/ /apiv1/public/index.php$is_args$args;
}

#12

Hi Victor,
I am getting another weird error now.

Message: The stream or file “/var/www/html/apiv1/src/…/logs/app.log” could not be opened: failed to open stream: Permission denied
File: /var/www/html/apiv1/vendor/monolog/monolog/src/Monolog/Handler/StreamHandler.php

For whole of the apiv1 folder I have given this chown -R nginx:nginx apiv1. Then I have created app.log in the logs folder too but still getting this error.


#13

Folder exists? Maybe you need to create.


#14

Hi Victor,
Ok let me explain first I tried to run the composer to install could not run on my sudo user then I back to root install it. Then I change the owner using chown command. Next the logs folder exist only the app.log dont exist. So using the root user I create the app.log file and then I change the owner to nginx. But it didnt work. Lastly I had to set the permission to 777 then it started to work which I feel is wrong and unsecured right.


#15

Hi Victor,
So what is your best solution to help on this? What permission should set for the log ?