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 = /
Related
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~~
I'm using Django in a Vhost and PHP in another, now I want to make one file with the two things, a part for Django and another for PHP.
I've read this question but it's not helping me.
How to run django and wordpress on NGINX server using same domain?
this one didn't help either
How can I configure nginx to serve a Django app and a Wordpress site?
This is part of what I have in the PHP config:
root /var/www/agendav/web/public;
# Add index.php to the list if you are using PHP
index index.php;
location ~ ^(.+\.php)(.*)$ {
try_files $fastcgi_script_name =404;
include /etc/nginx/fastcgi_params;
fastcgi_split_path_info ^(.+\.php)(.*)$;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
This is the full Django part:
upstream django {
server unix:///tmp/mysite.sock;
}
root /path/to/project/folder;
charset utf-8;
index index.html index.htm index.php;
if ($allowed_country = no) {
return 444;
}
# max upload size
client_max_body_size 75M;
# Django media
location /media {
alias /path/to/mysite/media/;
}
location /static {
alias /path/to/mysite/static/;
}
location ~ /cal/.*\.php$ {
root /var/www/agendav/web/public;
index index.php;
try_files $uri $uri/ /index.php/login =404;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
}
# Finally, send all non-media requests to the Django server.
location / {
uwsgi_pass pass;
include /path/to/uwsgi_params;
}
location /.well-known/acme-challenge/ {
allow all;
}
And I'm trying to add this to the other conf file (that's already working)
location ~ /cal/.*\.php$ {
root /var/www/agendav/web/public;
index index.php;
try_files $uri $uri/ /index.php/login =404;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
}
for what I've read, that should do, but all I've getting is a 404 error, or a blank page and no readings in the nginx log or even in the agendav(the app in PHP) ones.
What am I doing wrong?
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.
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;
}
My nginx configuration for laravel
server {
listen 80;
server_name app.dev;
rewrite_log on;
root /var/www/l4/angular;
index index.html;
location /{
# URLs to attempt, including pretty ones.
try_files $uri $uri/ /index.php?$query_string;
}
location /lara/ {
index index.php;
alias /var/www/l4/public/;
}
# Remove trailing slash to please routing system.
if (!-d $request_filename) {
rewrite ^/(.+)/$ /$1 permanent;
}
location ~ ^/lara/(.*\.php)$ {
alias /var/www/l4/public/$1;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
Laravel route:
Route::get('/', function()
{
return View::make('index');
});
Route::get('x', function()
{
return "alpha";
});
my problem is,"http://app.dev/lara/index.php" is working but "http://app.dev/lara" and lara/x is not working.
In a nutshell, make the following edits. An explanation of why is below.
Replace
try_files $uri $uri/ /index.php?$query_string;
with
try_files $uri $uri/ /lara/index.php?$query_string;
Replace the last location directive with this
location ~ /lara/(.*)$ {
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME "/var/www/l4/public/index.php";
fastcgi_param REQUEST_URI /$1;
}
Restart nginx.
Now the why. I spotted a couple of mistakes with your nginx config. First, /index.php?$query_string in the try_files directive should be /lara/index.php?$query_string, otherwise nginx will try a request like http://app.dev/lara as /var/www/l4/angular/index.php?, which leads no where (unless you have an index.php there, and even is it will be served as text, not through fpm).
The second has to do with the location ~ ^/lara/(.*\.php)$ directive. I think restricting it to URIs that end with .php is wrong, because it won't work for http://app.dev/lara/x, which will make nginx only search for /var/www/l4/public/x, returning 404 of course. Changing the regex to ^/lara/(.*)$ should do the job of catching /lara/x. Now the fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; directive is erroneous because for http://app.dev/lara/x, SCRIPT_FILENAME is /var/www/l4/public/x/lara/x, and removing the $1 in the alias directive won't make it any better. Instead, make the fastcgi_param like this fastcgi_param SCRIPT_FILENAME "/var/www/l4/public/index.php";, remove the alias directive, it's useless now, then move include fastcgi_params; above the fastcgi_param so it won't override SCRIPT_FILENAME value.
Done? Not yet :). Trying /lara/x will show a Laravel routing error, because it tries to find the route lara/x instead of x, this is because you're including fastcgi_params. Just add fastcgi_param REQUEST_URI /$1; after SCRIPT_FILENAME param directive. Now it should be working fine. Don't forget to restart nginx :).