First of all, this is not a duplicate, I've tried every possible variation of the answers already on Stack Overflow.
That being said, I'm facing a common issue, namely : My php code, supposed to execute through php-fpm is instead shown as text in the source of my php file.
Here's my config :
server {
listen 80;
server_name mywebsite.com;
root /var/www/old_bny;
default_type text/html;
location ~ \.php$ {
fastcgi_param REQUEST_METHOD $request_method;
#try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
This is only the last of many attempts... Any hints on how to debug/fix would be much appreciated.
Related
In this nginx configuration:
server {
server_name site.example.com;
index index.html index.php;
location / {
root /projects/proj1/frontend;
}
location /api/v1.0/ {
root /projects/proj1;
try_files $uri /api/v1.0/index.php$is_args$args;
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass php:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
}
Why every url that contains site.example.com is served by the first location, even site.example.com/api/v1.0/ ? Many thanks in advance.
EDIT: I've already tried to invert the order, so first /api/v1.0/ and second /, but with no luck.
EDIT2: Trying with curl the response is correct, but chrome keep using caching even in incognito...
That's the expected behavior in nginx. location / basically matches everything. If you want to match / only, use exact location: location = /
Something wrong with index:
It doesn't work out. The success one is like this:
The config of nginx is right. So, why can't I see the page with correct css style?
I finally get the answer.The reason is something wrong with my nginx.
nginx before:
location / {
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass 127.0.0.1:9000;
#fastcgi_pass unix:/var/run/php5-fpm.sock;
try_files $uri =404;
}
nginx now:
location / {
# Redirect everything that isn't a real file to index.php
try_files $uri $uri/ /index.php$is_args$args;
}
location ~ \.php$ {
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass 127.0.0.1:9000;
#fastcgi_pass unix:/var/run/php5-fpm.sock;
try_files $uri =404;
}
That's the reason! I didn't handle those static resources forwarding right.So, the js/css files are transmitted to fastcgi ,and then they are all changed to txt.
Thanks for all volunteers under this question!Both of you really do great help to me~~
Nginx returns a 404 when I query for an URL with a "path info" appended after the script name, e.g. http://example.com/index.php/hello.
Here is my config:
server {
listen 80;
root #PROJECT_ROOT#/;
index index.php index.html;
location ~ \.php$ {
fastcgi_pass unix:#PHP_FPM_SOCK#;
include fastcgi_params;
fastcgi_read_timeout 300s;
}
}
I don't get why \.php$ doesn't match that URL, and I've tried searching for similar problems but can't find anything useful.
Use
location ~ ^.+.php {
fastcgi_split_path_info ^(.+?.php)(/.*)$;
to match a .php in the uri split the parameters
This works for me:
location ~ \.php {
# Split the path appropriately
fastcgi_split_path_info ^(.+?\.php)(/.*)$;
# Work around annoying nginx "feature" (https://trac.nginx.org/nginx/ticket/321)
set $path_info $fastcgi_path_info;
fastcgi_param PATH_INFO $path_info;
# Make sure the script exists.
try_files $fastcgi_script_name =404;
# Configure everything else. This part may be different for you.
fastcgi_pass localhost:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
I've got a problem with Nginx. I'm just learning it, so it don't know fix this issue.
One of my plugins is trying to POST to a specific url that ends with a 'PHP'-extension.
The file isn't location in the root of the folder: 'web'. But in the directory:
web/plugins/moxiemanager/api.php. But I'm always receiving a 405.
What do I have to change in the configurations?
Thanks in advance.
My Nginx configurations:
server {
listen 80;
server_name kevin.dev;
root /var/www/html/kevin/web;
location / {
try_files $uri #rewriteapp;
}
location #rewriteapp {
rewrite ^(.*)$ /app.php/$1 last;
}
location ~ ^/(api|app|app_dev|config)\.php(/|$) {
fastcgi_buffer_size 128k;
fastcgi_buffers 4 256k;
fastcgi_busy_buffers_size 256k;
#fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_pass 127.0.0.1:9000;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param HTTPS off;
}
error_log /var/log/nginx/kevin_error.log;
access_log /var/log/nginx/kevin_access.log;
}
This may not be the best way to do it, I'm still finding my way with nginx, but it worked for me just now.
I duplicated my existing location block for app.php to cover the moxia script, so for me adding this did the job...
location ~ moxiemanager/api.php$ {
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SERVER_PORT 80;
include /etc/nginx/fastcgi_params;
}
that covers urls ending with moxiemanager/api.php. I'm not 100% sure of the security implications of doing it like this though.
I am trying to block the apc.php file on my webserver. If do the following it works but I am thinking there is a better way to do this and put the deny/allow rule below the general location ~ .php$ block. It doesnt seem right to have to have two blocks with the fastcgi params.
#Block to apc.php
location ~ /apc.php {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
allow 192.168.3.0/24;
deny all;
}
# use fastcgi for all php files
location ~ \.php$
{
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
Since "deny" not allowed in if statement, you can use nested location like location / { location /uri/ {} }, however, it is not encouraged in Nginx manual:
"While nested locations are allowed by the configuration file parser,
their use is discouraged and may produce unexpected results."