# Slim settings on nginx

**URL:** https://discourse.slimframework.com/t/slim-settings-on-nginx/2445
**Category:** Questions
**Created:** [May 31, 2018, 7:48pm UTC](https://discourse.slimframework.com/t/slim-settings-on-nginx/2445 "2018-05-31T19:48:40Z")
**Posts on this page:** 16
**Page:** 1

<div class="post-metadata">

### Author: ![newbie14](https://avatars.discourse-cdn.com/v4/letter/n/3d9bf3/32.png) [@newbie14](https://discourse.slimframework.com/u/newbie14)
#### Post date: [May 31, 2018, 7:48pm UTC](https://discourse.slimframework.com/t/slim-settings-on-nginx/2445/1 "2018-05-31T19:48:40Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![victor](https://avatars.discourse-cdn.com/v4/letter/v/3e96dc/32.png) [@victor](https://discourse.slimframework.com/u/victor)
#### Post date: [June 4, 2018, 1:50pm UTC](https://discourse.slimframework.com/t/slim-settings-on-nginx/2445/2 "2018-06-04T13:50:40Z")

</div>

Try root instead of alias

---

<div class="post-metadata">

### Author: ![newbie14](https://avatars.discourse-cdn.com/v4/letter/n/3d9bf3/32.png) [@newbie14](https://discourse.slimframework.com/u/newbie14)
#### Post date: [June 4, 2018, 4:21pm UTC](https://discourse.slimframework.com/t/slim-settings-on-nginx/2445/3 "2018-06-04T16:21:25Z")

</div>

Hi Victor,  
I have tried is the same still.

---

<div class="post-metadata">

### Author: ![victor](https://avatars.discourse-cdn.com/v4/letter/v/3e96dc/32.png) [@victor](https://discourse.slimframework.com/u/victor)
#### Post date: [June 4, 2018, 5:26pm UTC](https://discourse.slimframework.com/t/slim-settings-on-nginx/2445/4 "2018-06-04T17:26:00Z")

</div>

```
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,

---

<div class="post-metadata">

### Author: ![newbie14](https://avatars.discourse-cdn.com/v4/letter/n/3d9bf3/32.png) [@newbie14](https://discourse.slimframework.com/u/newbie14)
#### Post date: [June 4, 2018, 5:33pm UTC](https://discourse.slimframework.com/t/slim-settings-on-nginx/2445/5 "2018-06-04T17:33:19Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![victor](https://avatars.discourse-cdn.com/v4/letter/v/3e96dc/32.png) [@victor](https://discourse.slimframework.com/u/victor)
#### Post date: [June 4, 2018, 5:44pm UTC](https://discourse.slimframework.com/t/slim-settings-on-nginx/2445/6 "2018-06-04T17:44:26Z")

</div>

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

---

<div class="post-metadata">

### Author: ![newbie14](https://avatars.discourse-cdn.com/v4/letter/n/3d9bf3/32.png) [@newbie14](https://discourse.slimframework.com/u/newbie14)
#### Post date: [June 4, 2018, 5:52pm UTC](https://discourse.slimframework.com/t/slim-settings-on-nginx/2445/7 "2018-06-04T17:52:36Z")

</div>

> [@victor](#):
>
> location ~ ^/apiv1 { try\_files $uri $uri/ /apiv1/public/index.php$is\_args$args; }

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

---

<div class="post-metadata">

### Author: ![newbie14](https://avatars.discourse-cdn.com/v4/letter/n/3d9bf3/32.png) [@newbie14](https://discourse.slimframework.com/u/newbie14)
#### Post date: [June 4, 2018, 6:04pm UTC](https://discourse.slimframework.com/t/slim-settings-on-nginx/2445/8 "2018-06-04T18:04:03Z")

</div>

> [@newbie14](#):
>
> # Load configuration files for the default server block. include /etc/nginx/default.d/\*.conf;

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

---

<div class="post-metadata">

### Author: ![victor](https://avatars.discourse-cdn.com/v4/letter/v/3e96dc/32.png) [@victor](https://discourse.slimframework.com/u/victor)
#### Post date: [June 4, 2018, 9:26pm UTC](https://discourse.slimframework.com/t/slim-settings-on-nginx/2445/9 "2018-06-04T21:26:01Z")

</div>

> [@newbie14](#):
>
> ```
> error_page 404 /404.html; 
> location = /40x.html { 
> } 
> error_page 500 502 503 504 /50x.html; 
> location = /50x.html { 
> }
> 
> ```

Remove this and you will see the correct error in error\_log.

---

<div class="post-metadata">

### Author: ![newbie14](https://avatars.discourse-cdn.com/v4/letter/n/3d9bf3/32.png) [@newbie14](https://discourse.slimframework.com/u/newbie14)
#### Post date: [June 5, 2018, 2:48am UTC](https://discourse.slimframework.com/t/slim-settings-on-nginx/2445/10 "2018-06-05T02:48:47Z")

</div>

> [@victor](#):
>
> 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; }

Hi Victor,  
I did like you suggested. Now when I got just to [http://myip/apiv1](http://myip/apiv1) I get 403 forbidden. Next say I type [http://myip/apiv1/token](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;
    #}
    
}

```

---

<div class="post-metadata">

### Author: ![victor](https://avatars.discourse-cdn.com/v4/letter/v/3e96dc/32.png) [@victor](https://discourse.slimframework.com/u/victor)
#### Post date: [June 5, 2018, 6:08am UTC](https://discourse.slimframework.com/t/slim-settings-on-nginx/2445/11 "2018-06-05T06:08:08Z")

</div>

This:

> [@newbie14](#):
>
> location ~ ^/apiv1 { try\_files $uri $uri/ /index.php$is\_args$args; }

is:

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

```

---

<div class="post-metadata">

### Author: ![newbie14](https://avatars.discourse-cdn.com/v4/letter/n/3d9bf3/32.png) [@newbie14](https://discourse.slimframework.com/u/newbie14)
#### Post date: [June 5, 2018, 3:49pm UTC](https://discourse.slimframework.com/t/slim-settings-on-nginx/2445/12 "2018-06-05T15:49:01Z")

</div>

> [@victor](#):
>
> location ~ ^/apiv1 { try\_files $uri $uri/ /apiv1/public/index.php$is\_args$args; }

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.

---

<div class="post-metadata">

### Author: ![victor](https://avatars.discourse-cdn.com/v4/letter/v/3e96dc/32.png) [@victor](https://discourse.slimframework.com/u/victor)
#### Post date: [June 5, 2018, 9:55pm UTC](https://discourse.slimframework.com/t/slim-settings-on-nginx/2445/13 "2018-06-05T21:55:05Z")

</div>

Folder exists? Maybe you need to create.

---

<div class="post-metadata">

### Author: ![newbie14](https://avatars.discourse-cdn.com/v4/letter/n/3d9bf3/32.png) [@newbie14](https://discourse.slimframework.com/u/newbie14)
#### Post date: [June 6, 2018, 10:30am UTC](https://discourse.slimframework.com/t/slim-settings-on-nginx/2445/14 "2018-06-06T10:30:55Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![newbie14](https://avatars.discourse-cdn.com/v4/letter/n/3d9bf3/32.png) [@newbie14](https://discourse.slimframework.com/u/newbie14)
#### Post date: [June 12, 2018, 9:12am UTC](https://discourse.slimframework.com/t/slim-settings-on-nginx/2445/15 "2018-06-12T09:12:45Z")

</div>

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

---

<div class="post-metadata">

### Author: ![SomeT](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/somet/32/615_2.png) [@SomeT](https://discourse.slimframework.com/u/SomeT)
#### Post date: [July 12, 2018, 4:26pm UTC](https://discourse.slimframework.com/t/slim-settings-on-nginx/2445/16 "2018-07-12T16:26:56Z")

</div>

I find this site good and useful for nginx config: [https://nginxconfig.io/](https://nginxconfig.io/)
