Nginx doesn't server subfolder api application (php-fpm) - php

I have following folder structure for my application
- css
- app
- js
- ...
- server (codeigniter)
-- system
-- application
-- index.php
- index.html (angularJS app)
this is my nginx.conf
server {
listen 80;
listen [::]:80;
root /var/www/html/myapp.com/public_html;
index index.html;
server_name myapp.com www.myapp.com;
location / {
index index.html;
}
location /server/ {
index /server/index.php;
try_files $uri /server/index.php/$uri;
}
location /twitter/ {
index /twitter/index.php;
try_files $uri /twitter/index.php/$uri;
}
location ~ \.php {
fastcgi_split_path_info ^(.+?\.php)(/.*)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
include fastcgi_params;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
#deny access to .htaccess files, if Apache's document root
#concurs with nginx's one
location ~ /\.ht {
deny all;
}
}
And this is the error i'm keep getting
2016/03/04 18:26:02 [error] 4577#0: *4 FastCGI sent in stderr: "Unable to open primary script: /home/valor/development/myapp.com/public_html/server/index.php (No such file or directory)" while reading response header from upstream, client: 127.0.0.1, server: myapp.com, request: "GET /server/api/users/isLoggedIn HTTP/1.1", upstream: "fastcgi://unix:/var/run/php5-fpm.sock:", host: "myapp.com", referrer: "http://myapp.com/"
2016/03/04 18:26:02 [error] 4577#0: *3 FastCGI sent in stderr: "Unable to open primary script: /home/valor/development/myapp.com/public_html/server/index.php (No such file or directory)" while reading response header from upstream, client: 127.0.0.1, server: myapp.com, request: "GET /server/api/categories HTTP/1.1", upstream: "fastcgi://unix:/var/run/php5-fpm.sock:", host: "myapp.com", referrer: "http://myapp.com/"
2016/03/04 18:26:03 [error] 4577#0: *3 FastCGI sent in stderr: "Unable to open primary script: /home/valor/development/myapp.com/public_html/server/index.php (No such file or directory)" while reading response header from upstream, client: 127.0.0.1, server: myapp.com, request: "GET /server/api/products/latest/16 HTTP/1.1", upstream: "fastcgi://unix:/var/run/php5-fpm.sock:", host: "myapp.com", referrer: "http://myapp.com/"
Page is displayed correctly i just can reach api resources in sub folder.
Can you guy help me out what i'm doing wrong?
If you need any additional information's please let me know and i will provide
Thank you

Based on user #semm0 (link)
This might be a permission problem, possibly caused by AppArmor. Please review this answer to see possible solution. I don't want to steal the answer, therefore linking to it.
EDIT: the problem might now occur from the php-fpm configuration. Please open /etc/php5/fpm/pool.d/www.conf with your favorite editor and uncomment the following lines:
listen.owner = www-data
listen.group = www-data
listen.mode = 0660
and don't forget to restart the php server with:
service php5-fpm restart

Related

Connection reset by peer while reading response header from upstream (NGINX)

Im trying to run the local project (php phalcon) using nginx on macOS but it seems nginx can't load the project correctly.
Here is my nginx log given :
2023/01/12 11:38:04 [error] 6170#0: *1 kevent() reported about an closed connection (54: Connection reset by peer) while reading response header from upstream, client: 127.0.0.1, server: bahana.front, request: "GET / HTTP/1.1", upstream: "fastcgi://127.0.0.1:9074", host: "127.0.0.1:7002"
2023/01/12 11:38:05 [error] 6170#0: *1 kevent() reported about an closed connection (54: Connection reset by peer) while reading response header from upstream, client: 127.0.0.1, server: bahana.front, request: "GET /favicon.ico HTTP/1.1", upstream: "fastcgi://127.0.0.1:9074", host: "127.0.0.1:7002", referrer: "http://127.0.0.1:7002/"
and here is my site nginx config : (for loading the php phalcon project)
server {
listen 7002;
root /Applications/XAMPP/xamppfiles/htdocs/bahana-front/bahana/public;
index index.php;
server_name bahana.front;
location / {
try_files $uri $uri/ /index.php;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass 127.0.0.1:9074;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
also the php-fpm work well in the background (port 9074) :
So, what did i miss for the configuration, im little stuck and keep getting closed connection at nginx log and HTTP Error 502 (Bad Gateway) when accessing the site at http://127.0.0.1:7002, Gonna need your help, thanks.

nginx1.8 Laravel 502 bad gateway error

SOLVED!
Using Virtualbox to setup Homestead vagrantbox
Here is the server configuration in the file dev.blog.com.conf
server {
listen 80;
server_name dev.blog.com;
root /vagrant/blog/public;
index index.php index.html index.htm;
location / {
index index.php index.html index.htm;
try_files $uri #rewrite;
}
location #rewrite {
rewrite ^ /index.php;
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi.conf;
}
access_log /vagrant/logs/blog.access.log;
error_log /vagrant/logs/blog.error.log;
}
And in VagrantFile I've uncommented the line 'config.vm.network'
In My windows hosts file I've add this line
192.168.33.10 dev.blog.com
I keep restarting Nginx or refreshing the page,but still got the 502 bad gatway.
It's there some permission problem about my folder or other problems?
Log
2017/02/17 02:16:54 [error] 1717#0: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 192.168.33.1, server: dev.blog.com, request: "GET / HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "192.168.33.10"
2017/02/17 02:17:02 [error] 1717#0: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 192.168.33.1, server: dev.blog.com, request: "GET / HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "192.168.33.10"
I figured out the Problem,The new php configuration had been modified a little bit,so in the
vim /etc/php5/fpm/pool.d/www.conf
so the listen line should be listen=9000 instead of listen = /var/run/php5-fpm.sock

connect() to unix:/tmp/php-fpm.sock failed (2: No such file or directory)

On my vagrant box , centtos 7, i had installed php7.0.3 and nginx 1.9.12. the php config www.conf. this file in the /usr/local/php/etc/php-fpm.d/ directory, Its configuration like this
user = www
group = www
;listen = /tmp/php-fpm.sock
listen = 127.0.0.1:9000
listen.owner = www
listen.group = www
and the nginx config. the file vm.demo.com.conf in the /usr/local/nginx/conf/vhost/ directory. Its configuration like this
server {
listen 80;
server_name vm.demo.com;
index index.html index.htm index.php;
root /data/wwwroot/demo;
location / {
if (-f $request_filename/index.html){
rewrite (.*) $1/index.html break;
}
if (-f $request_filename/index.php){
rewrite (.*) $1/index.php;
}
if (!-f $request_filename){
rewrite (.*) /index.php;
}
}
location ~ .*\.(php|php5)?$ {
#fastcgi_pass unix:/tmp/php-fpm.sock;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi.conf;
}
access_log logs/demo.log main;
}
in the nginx conf, if i use fastcgi_pass 127.0.0.1:9000 and the php-fpm.conf listen 127.0.0.1:9000, They are working properly
bug if i use the socket configuration:
php www.conf
user = www
group = www
listen = /tmp/php-fpm.sock
listen.owner = www
listen.group = www
nginx vm.demo.com.conf
location ~ .*\.(php|php5)?$ {
fastcgi_pass unix:/tmp/php-fpm.sock;
fastcgi_index index.php;
include fastcgi.conf;
}
the nginx show 502 Bad gateway. in the nginx error.log show this.
2016/03/14 21:17:04 [crit] 4208#0: *5 connect() to unix:/tmp/php-fpm.sock failed (2: No such file or directory) while
connecting to upstream, client: 192.168.1.101, server: vm.demo.com, request: "GET /test.php HTTP/1.1",
upstream: "fastcgi://unix:/tmp/php-fpm.sock:", host: "vm.demo.com"
2016/03/14 21:17:04 [crit] 4208#0: *5 connect() to unix:/tmp/php-fpm.sock failed (2: No such file or directory) while
connecting to upstream, client: 192.168.1.101, server: vm.demo.com, request: "GET /test.php HTTP/1.1",
upstream: "fastcgi://unix:/tmp/php-fpm.sock:", host: "vm.demo.com"
2016/03/14 21:18:01 [crit] 4208#0: *5 connect() to unix:/tmp/php-fpm.sock failed (2: No such file or directory) while
connecting to upstream, client: 192.168.1.101, server: vm.demo.com, request: "GET /test.php HTTP/1.1",
upstream: "fastcgi://unix:/tmp/php-fpm.sock:", host: "vm.demo.com"
when i check the /tmp/php-fpm.sock file, it's existing
[root#vbox1 vhost]# ll /tmp/php-fpm.sock
srw-rw---- 1 www www 0 Mar 14 21:06 /tmp/php-fpm.sock
this are php and nginx running status:
[root#vbox1 vhost]# systemctl status php-fpm.service
● php-fpm.service - The PHP FastCGI Process Manager
Loaded: loaded (/etc/systemd/system/php-fpm.service; enabled; vendor preset: disabled)
Active: active (running) since Mon 2016-03-14 21:06:27 CST; 42min ago
Process: 4189 ExecStop=/bin/kill -SIGINT $MAINPID (code=exited, status=0/SUCCESS)
Main PID: 4198 (php-fpm)
CGroup: /system.slice/php-fpm.service
├─4198 php-fpm: master process (/usr/local/php/etc/php-fpm.conf)
├─4199 php-fpm: pool www
└─4200 php-fpm: pool www
Mar 14 21:06:27 vbox1 systemd[1]: Started The PHP FastCGI Process Manager.
Mar 14 21:06:27 vbox1 systemd[1]: Starting The PHP FastCGI Process Manager...
[root#vbox1 vhost]# systemctl status nginx.service
● nginx.service - nginx
Loaded: loaded (/etc/systemd/system/nginx.service; enabled; vendor preset: disabled)
Active: active (running) since Mon 2016-03-14 21:06:35 CST; 42min ago
Process: 4180 ExecStop=/usr/local/nginx/sbin/nginx -s quit (code=exited, status=0/SUCCESS)
Process: 4206 ExecStart=/usr/local/nginx/sbin/nginx (code=exited, status=0/SUCCESS)
Main PID: 4207 (nginx)
CGroup: /system.slice/nginx.service
├─4207 nginx: master process /usr/local/nginx/sbin/nginx
├─4208 nginx: worker process
└─4209 nginx: worker process
Mar 14 21:06:35 vbox1 systemd[1]: Starting nginx...
Mar 14 21:06:35 vbox1 systemd[1]: Started nginx.
Can someone help me? If anyone encountered the same problem
I have met the same problem.
And I just change the socket path to another directory but '/tmp',
Then everything goes OK.
Basically, the problem arises with ONE process (php-fpm) setting up the sock in the /tmp path, but it would not be visible by ANOTHER process (nginx).
There is a setting in the service config, which would block files created by different process, even though using the same user.
Find your service config file (ex: /etc/systemd/system/myapp.service) and then on add this setting: PrivateTmp=No to both your services (php and nginx).
[Service]
...
PrivateTmp=No
more info about systemd units here:
https://www.freedesktop.org/software/systemd/man/systemd.unit.html
I have found a config (php5) that continues to use the unix socket and does not involve editing any other config files (except the nginx default site file).
location ~ \.php$ {
fastcgi_pass unix:/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
# \Add following to use sqlite as db.
fastcgi_param DB_CONNECTION sqlite;
}
Note:- the unix socket is not at /run/php/ (though it is for php7).
The include fastcgi_params; is required to prevent the php file downloading instead of being interpreted.
For completeness the following is my entire default file.
index index.php index.html index.htm;
server {
listen 80;
server_name sponk.co.uk;
rewrite_log on;
root /vagrant/public_html;
try_files $uri $uri/ /index.php$is_args$args;
location ~ \.php$ {
fastcgi_pass unix:/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
}
You need to change the listen.owner and the listen.group
listen.owner = nginx
listen.group = nginx
In my case in this file: /etc/php-fpm.d/www.conf
The socket must be the same in you nginx block (like vhost in apache). I also moved the socket under /var/sockets
You could try changing user in /usr/local/php/etc/php-fpm.d/www.conf to nginx and restart service.
user = nginx
group = nginx
listen = /tmp/php-fpm.sock
For all those who use Homestead inside Vagrant:
I solved simply reloading vagrant with provision flag:
from project root:
cd Homestead
and then:
vagrant reload --provision
in this manner recompile all configurations
I had this problem, you can do this:
chmod 777 yourpath/php-fpm.sock
This worked for me.

Windows 10 - Nginx & Symfony 3 configs

I am trying to setup my environment using Nginx and a fresh project from Symfony 3, both work fine on their own (either starting Nginx by launching php-cgi.exe -b 127.0.0.1:9000 or starting the Symfony 3 project with server:run), however, I can't get Symfony working through Nginx.
The error message I get in my log goes like this:
2016/02/21 00:36:33 [error] 6260#1732: *1 upstream timed out (10060: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond) while connecting to upstream, client: 127.0.0.1, server: localhost, request: "GET / HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "localhost"
And this is my code in nginx.conf for the server section so far:
server {
listen 80;
server_name localhost;
root www/projects/mysite.com/web;
rewrite ^/app\.php/?(.*)$ /$1 permanent;
location / {
index app.php;
try_files $uri #rewriteapp;
}
location #rewriteapp {
rewrite ^(.*)$ /app.php/$1 last;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
location ~ ^/(app|app_dev|config)\.php(/|$) {
fastcgi_pass 127.0.0.1:9000;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
I have placed the 50x.html file in my /web folder and it does show up correctly after the timeout...
I can understand you are using Unix / Linux machine. This is because you haven't enabled (granted the privilege for upstream).
setsebool -P httpd_can_network_connect true
The upstream blocked by selinux by default. Please let me know if this fixes.
Thanks.

nginx rewrite issues

I just switched from apache to nginx, just for testing and I experience the following problem. I am using this config for nginx
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/share/nginx/html$fastcgi_script_name;
include fastcgi_params;
}
Now I have a php script makethumbs.php that automatically resize images displayed on my website. With apache, works just fine. With nginx I get this error:
2011/12/29 15:13:17 [error] 15548#0: *9 open() "/usr/share/nginx/html/makethumbs.php/0737438664-22.jpg" failed (20: Not a directory), client: 193.138.192.81, server: www.escortele.eu, request: "GET /makethumbs.php/0737438664-22.jpg?width=48&height=64&image=/members/escorte/0737438664-22.jpg HTTP/1.1", host: "escortele.eu:88", referrer: "http://escortele.eu:88/"
The problem is that it sees makethumbs.php as a directory and it should be a script not a directory.
I can't figure it out what rewrite rule to use, only for makethumbs.php so it acts like a script and not like a directory.
You should have pasted the rest of your configuration file because what you pasted has nothing to do with what you want.
That error is because you have "try_files $uri $uri/ /index.php;" somewhere in your config. You need to remove the $uri/ from that to fix the error you pasted.
The problem is not in
location / {
try_files $uri $uri/ /index.php;
}
That part is good. You need it.
Since you didn't post your entire config I can't be sure what you're problem is, but I can tell you what my problem was in the same type of situation. Following the recomendation of others, I had a location defined for image files as follows:
location ~* \.(jpg|jpeg|png|gif|css|js|ico)$ {
expires max;
log_not_found off;
}
That was the problem. Because Nginx uses the most specific location that matches, the URL ending with .jpg would match this location and this location doesn't tell it to use index.php. I just got rid of this location and it worked.
Additional tips:
define your root in the server block and not in location blocks.
I don't think you don't need "fastcgi_index index.php;"
You don't need to hard-code your document root:
change
fastcgi_param SCRIPT_FILENAME /usr/share/nginx/html$fastcgi_script_name;
to
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
This is the script I am using for thumbnails: http://escorte.pro:88/makethumbs.txt
My nginx config is as follows: http://escorte.pro:88/nginx.txt
The error I get is:
2014/08/27 14:59:07 [error] 20986#0: *86 open() "/usr/share/nginx/html/escorte.pro/makethumbs.php/0737835261-79.jpg" failed (20: Not a directory), client: 83.166.220.234, server: escorte.pro, request: "GET /makethumbs.php/0737835261-79.jpg?width=48&height=64&image=/members/escorte/0737835261-79.jpg HTTP/1.1", host: "escorte.pro:88", referrer: "http://escorte.pro:88/"
2014/08/27 14:59:07 [error] 20986#0: *87 open() "/usr/share/nginx/html/escorte.pro/makethumbs.php/0743844296-60.jpg" failed (20: Not a directory), client: 83.166.220.234, server: escorte.pro, request: "GET /makethumbs.php/0743844296-60.jpg?width=48&height=64&image=/members/escorte/0743844296-60.jpg HTTP/1.1", host: "escorte.pro:88", referrer: "http://escorte.pro:88/"
The makethumbs.php script works perfectly fine on apache
Any clues?
I hope this info si more complete than the previous...

Categories