NGINX fastcgi cannot get script name - nginx

I am setting up fastcgi on nginx. I sort of have it working. Inside my location block if i have factcgi_param SCRIPT_FILENAME /var/www/cgi-bin/hello.cgi; it works. However I don't want to hard code the link.
Setting fastcgi with the below configuration
factcgi_param SCRIPT_FILENAME /var/www/cgi-bin/$fastcgi_script_name;
I get the following error
cannot get script name are document_root and script_name or (script_file) set and is the script executable?
while reading response header upstream from upstream. client 127.0.0.1,
server _, request "get /cgi-bin/hello.cgi"
On the webpage I get 403 Forbidden
hello.cgi is executable and works when I hard code it.
My fastcgi_params

Related

nginx configuratio : alias and 404 error

My actual problem was that I wanted to make my "site.com/blog/index.php" direct to "/srvX/www/blog/caller/index.php". Althought it would be very straightforward to direct to "/srv/www/blog/index.php" using "root /srv/www/", that's not what I wanted. I discovered "alias", and it seem to do what I want.
1)First try :
server {
listen 80;
server_name _;
root /srv/www/blog/pages;
index index.php;
location /blog {
alias /srv/www/blog/caller;
}
}
There trying site.com/blog get me a 404 not found, and nothing pop into /var/log/nginx/error.log
1)Second try to know what happens :
If I change "alias /srv/www/blog/caller;" to a bad path, let say "alias /srvX/www/blog/caller;" I actually got the same behaviour in my browser, but
I can see in /var/log/nginx/error.log :
[error] 7229#0: *1 "/srvX/www/blog/caller/index.php" is not found (2: No such file or directory), client: 192.168.1.200, server: 192.168.1.221, request: "GET /blog/ HTTP/1.1", host: "192.168.1.221"
Conclusion : I don't know what's hapenning there : it seem clear that nginx get the file in my first try, but it sends the 404 error to the browser with no reason I could think of, while when specyfiyng a wrong path, it tells me right away. :/*
edit
Well, I found the solution. Basically it totally works from nginx, the problem was from php-fpm who lose his mind when using alias into nginx. What you need to do is doing a sublocation of aliased locations adding :
location ~ \.php$ {
fastcgi_pass unix:/var/run/php5-fpm.sock;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $request_filename;
}
Now it works.
The fact that nginx was giving a 404 error without anything in the nginx's logs, was that php-fpm was the one failing to serve.
The problem is that you have no instructions on how to deal with the php script. To solve this issue the following:
Add the following code to your nginx.conf file within the server tags or if you have created that in your conf.d folder add it to that file.
location / {
try_files $uri $uri/ /index.php?q=$request_uri;
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
That will solve that problem but also in the file:
/etc/php-fpm.d/www.conf
Ensure that listen.owner is set to listen.owner = nginx
Ensure that listen.group is set to listen.group = nginx
Restart both services and it should work.
If not ensure your document root and all files with that directory are owned by the user nginx and the group nginx.
If not you can do this by using the following:
chown -R nginx:nginx documentroot
And keep doing that but adding /* each time until you reach an error.
Hope everything works out for you!!

Getting Fatal Errors in Nginx to Be Shown in the Browser

EDIT:
I have done everything suggested in the duplicate answers and nothing works. Here is my PHP info:
This is a specific issue with nginx and stderr messages and is not a generic "how do I turn on PHP error reporting" issue.
While developing I'm getting blank pages in my browser whenever I create a fatal error in PHP with a typo or just my bad programming ;). It's super annoying for me to have to view the raw nginx error log file to see the fatal errors and find the line numbers where they are. I can't seem to find how to make nginx display PHP fatal errors in the browser. Here is the relevant part of my nginx config:
location #fpm {
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_NAME index.php;
fastcgi_param SCRIPT_FILENAME $document_root/index.php;
fastcgi_param PATH_INFO $path_info;
}
Here's an example error that shows up in my error log and then results in a blank browser page:
2014/01/04 14:53:52 [error] 20082#0: *403 FastCGI sent in stderr:
"PHP message: PHP Fatal error: Cannot redeclare class ClassName in FilePath on line 356"
while reading response header from upstream, client: 192.168.1.10,
server: servername, request: "GET URLPATH HTTP/1.1",
upstream: "fastcgi://unix:/var/run/php5-fpm.sock:", host: "host",
referrer: "referer"
Here is my PHP-FPM conf:
http://pastebin.com/QkCTbBYj
And my PHP-FPM pool conf:
http://pastebin.com/TZfZ8d7G
And my PHP-FPM php.ini:
http://pastebin.com/RsXRxduf
I would love if anyone could shed some light on what I could do to get these errors to show up!
try setting catch_workers_output to yes in pool config:
; Redirect worker stdout and stderr into main error log. If not set, stdout and
; stderr will be redirected to /dev/null according to FastCGI specs.
; Note: on highloaded environement, this can cause some delay in the page
; process time (several ms).
; Default Value: no
catch_workers_output = yes
if that doesn't work you might have this bug:
https://bugs.php.net/bug.php?id=61045

Nginx is slow... or I'm missing something?

I've got a fastcgi applicaiton served by nginx. The site configuration is very basic:
upstream foo {
server unix:/var/run/fastcgi/foo.sock;
}
server {
listen 8080;
server_name _;
root /usr/share/nginx/www;
index index.html index.htm;
location / {
include fastcgi_params;
fastcgi_param PATH_INFO $fastcgi_script_name;
fastcgi_pass foo;
}
}
The app itself is quite fast, it's processing typical request in about 3-5 miliseconds - I can see this in the app log and in $upstream_response_time logged to access.log (this is not static files or anything like this, the app is just processing normal HTTP POSTs and returning application/json http response). However, total request time ($request_time variable) is very long, it's 140-160 miliseconds.
What's nginx doing that takes so much time? I suspect it might be opening/closing socket connection for each request or something like this (but 140 miliseconds?!?), how can I trace the cause of the issue?

zero size shared memory zone "proxied" in nginx

I installed nginx with nginx-extras to get (Http Upload Progress Module).
Then i tried to use it following this documentation ( example on the bottom of the page ) -> http://wiki.nginx.org/HttpUploadProgressModule. After inserting this one line (track_uploads proxied 30s;) in my configuration , i get the following error
nginx: [emerg] zero size shared memory zone "proxied"
Somewhere on the internet i found , that someone suggested to insert upload_progress proxied 10m; into nginx.conf , and after inserting it i started geting the following error:
nginx: [emerg] the size 10485760 of shared memory zone "proxied"
conflicts with already declared size 0 in /etc/nginx/nginx.conf:75
This the part of the config , where are inserted this line....
location ~ \.php$ {
include /etc/nginx/fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME /home/cha0s/learnphp$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_script_name;
track_uploads proxied 1m;
}
So the question is , what should i write in nginx.conf to resolve this error ? Thanks in advance.
You should put upload_progress proxied 1m; before your server block, like it is in the example.

Send additional header to FastCGI backend with nginx

I use nginx with several fastcgi backends (php-cgi, mod-mono-fastcgi4). Now I need to sent an additional http header to the fastcgi backend, basically the same as proxy_set_header does when using nginx as reverse proxy. But to my findings, there is no such thing as fastcgi_set_header in nginx.
Somebody got any ideas how to do this anyways? I dont want to use additional nginx modules as the solution muste be easily deployable on a wide range of customer systems.
I took a quick look at the manual and I think the closest you will find is passing fastcgi parameters:
The request headers are transferred to the FastCGI-server in the form of parameters. In the applications and the scripts run from the FastCGI-server, these parameters are usually accessible in the form of environment variables. For example, the header "User-agent" is transferred as parameter HTTP_USER_AGENT. Besides the headers of the HTTP request, it is possible to transfer arbitrary parameters with the aid of directive fastcgi_param.
http://wiki.nginx.org/HttpFcgiModule#Parameters.2C_transferred_to_FastCGI-server.
fastcgi_param
syntax: fastcgi_param parameter value
http://wiki.nginx.org/HttpFcgiModule#fastcgi_param
The URLs to the nginx wiki articles above are broken.
nginx exposes request header values via variables prefixed with $http_, so a request header of HTTP_USER_AGENT is available via $http_user_agent.
Likewise a request header named CHICKEN_SOUP would be available via $http_chicken_soup.
The example below shows how to pass the the Authorization HTTP request header to PHP scripts running under php-fpm (PHP FastCGI process manager).
location ~ \.php$ {
fastcgi_pass unix:/path/to/socket;
fastcgi_index index.php;
fastcgi_param HTTP_AUTHORIZATION $http_authorization;
... other settings
}
Nginx now has:
fastcgi_pass_header 'Cache-Control: no-cache, must-revalidate';
Which can be used in your location rules if you are adding headers which aren't already specified in your request. By default fastcgi uses:
fastcgi_pass_request_headers on;
Which will pass all incoming Headers from the request to fastcgi.
You can do this with the third party module ngx_headers_more. After building nginx with this module included, you can do the following in your configuration:
location / {
more_set_input_headers 'Foo: bar baz';
...
}

Resources