I am trying to achieve the following result with an nginx configuration:
A PHP app is running in a subdirectory of a server, lets say
server.com/app/. Files in images/ and styles/ (for example) should be accessible, php files in api/ should be executed, and in all other cases nginx should pass the whole string after app/ to PHP as a GET variable, say path.
I really have no clue what I am doing here, and I can not seem to find anything useful for this on the web, so if you can chip in, thank you.
I am running php5-fpm currently like this:
location /app {
index index.html index.php;
access_log /{...}/access.log;
error_log /{...}/error.log;
location ~ \.php {
try_files $uri = 404;
fastcgi_pass php5-fpm-sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
Please ask if you need any more details.
EDIT
For now I found that this works
location /{path}/ {
index index.php;
access_log /{path}/access.log;
error_log /{path}/error.log;
location ~\.php {
try_files $uri = 404;
fastcgi_pass php5-fpm-sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ {
try_files $uri $uri/ /{path}/index.php?path=$uri;
}
}
However I am worried that this might allow unwanted file access. Any comments?
You can probably simplify it by moving the try_files directive out of the location sub-block so that your config file ends up looking like:
location /app {
index index.php;
try_files $uri $uri/ /app/index.php?path=$uri;
access_log /{path}/access.log;
error_log /{path}/error.log;
location ~\.php {
try_files $uri =404;
fastcgi_pass php5-fpm-sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
The key thing is the try_files directive - nginx will try each location in the order specified. $uri looks for a file matching the exact path specified (so /api/random.php loads correctly because it's a file), $uri/ looks for a folder matching the path, and attempts to load the index from the folder, and finally /app/index.php?path=$uri loads the page /app/index.php. This is then picked up by the location ~\.php block and passed to php-fpm.
The main thing I'd be concerned about is that your access and error.log files would be publicly accessible by virtue of being stored in the web directory. If possible, shift them somewhere else (like /var/log maybe?)
Related
I'm new to nginx and want to configure it so the user can access URLs like
http://[ip_address]/dev/index.php/customer/account/login/
I think this may be related to using FastCGI to process the request and pass it to Magento. However, whenever I access it, i see a 404 message. I can confirm that the user running nginx and owning the directory and files is www-data. So it has access to it. I need help configuring nginx & FastCGI properly so the request loads the correct page.
All of my application is in dev/ folder. Here's the relevant chunk of default file in /etc/nginx/sites-available/defaul:
root /var/www;
# Add index.php to the list if you are using PHP
index index.php index.html index.htm index.nginx-debian.html;
server_name _ test.xxx.com;
location /dev/ {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ /index.php$is_args$args;
}
location /dev/app/ {
deny all;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
# With php-fpm (or other unix sockets):
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
# With php-cgi (or other tcp sockets):
#fastcgi_pass 127.0.0.1:9000;
}
Here is my nginx config. May help you.
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
fastcgi_read_timeout 900;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_index index.php;
include fastcgi_params;
}
I have an online web server where I have deployed multiple applications and they all have to share the domain, so my nginx config is a bit messy. The last time I changed it, the PHP files didn't load getting the error ("access denied to file") so I had to do some changes, now the config for one of the applications is like this:
location ^~ /vuelos_baratos {
root /home/gonzalo;
try_files $uri $uri/ /$uri/index.php /index.php$is_args$args $uri/index.php =404;
#root /usr/share/nginx/html;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include /etc/nginx/fastcgi_params;
include /etc/nginx/mime.types;
autoindex on;
}
and the urls of this application are like this:
mydomain.com/vuelos_baratos/index.php
mydomain.com/vuelos_baratos/style.css
...
and so on.
And the PHP files and everything is working fine, except that If I try to acces:
mydomain.com/vuelos_baratos/image.png
I get the binary data of the image, I discovered that that is because the headers for all files under "vuelos_baratos" are set to type: text/html
How can I fix this?
I don't think that nginx is responsible for determining the content type in your current configuration, as you pass everything to php-fpm.
You could divide the static and dynamic sections into separate locations, for example:
location ^~ /vuelos_baratos {
include /etc/nginx/mime.types;
root /home/gonzalo;
index index.php;
try_files $uri $uri/ /index.php$is_args$args;
location ~ \.php$ {
try_files $uri =404;
include /etc/nginx/fastcgi_params;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
fastcgi_param SCRIPT_FILENAME $request_filename;
}
}
Which should mean that /vuelos_baratos/image.png is processed by nginx and that /etc/nginx/mime.types is used to determine the Content Type.
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?
Based on this question How to install symfony2 app in a subdirectory in nginx
I've created symfony3 application that works in subdirectory called bcms4. I've manged to make php work with PHP-FPM but I have probelms with assets. When I want to GET asset it directs the request to app_dev and shows 404 because obviosly the path does not exist.
My question is how to make assets not to be proccesed by app_dev but downloaded as supposed?
So when I enter
test.localhost/s/asdfad -> it runs symfony
test.localhost/asdf -> it runs other app living in main dir
test.localhost/s/assets/css/test.css -> it will show file in directory /var/www/test.localhost/bcms4/web/assets/css/test.css
My nginx config:
server {
listen 80;
root /var/www/test.localhost;
index index.html index.htm index.php;
# Make site accessible from http://localhost/
server_name test.localhost;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
# Uncomment to enable naxsi on this location
# include /etc/nginx/naxsi.rules
}
location ~ ^/s(/.*)$ {
try_files /s/web$1 /web$1 #sf2dev =404;
}
location #sf2dev {
expires off;
fastcgi_pass unix:/var/run/php5-fpm.sock;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME /var/www/test.localhost/bcms4/web/app_dev.php;
fastcgi_param SCRIPT_NAME /s/app_dev.php;
fastcgi_param REQUEST_URI /s$1;
}
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
# NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
# With php5-fpm:
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
fastcgi_param DOCUMENT_ROOT $realpath_root;
fastcgi_intercept_errors on;
}
}
After hours of trying I've managed to figure it out with little hack.
This is what I've added to my config file
location ~ ^/s(/.*).\w{1,5}$ {
rewrite ^/s(/.*) /bcms4/web$1 break;
return 404;
}
It'll rewrite files that has prefix /s and extension to directory where they are actually.
Maybe it will help someone. I'll leave question open for a while maybe someone has better solution cause it's seems hacky for me.
I can't figure out how to make the Nginx config file locate my Codeigniter app. Serving PHP on this server is not the problem b/c if I put a php file in my root directory, I can echo "hello world";.
Here's my Nginx config file which is nearly verbatim from this tutorial. Note that I've manipulated many of these parameters and none had an effext so I'm wondering whether I need to look beyond this file to get it to work?:
server {
listen 80;
server_name www.mysite.com mysite.com;
root /var/www;
index index.html index.php index.htm;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
location / {
try_files $uri $uri/ /index.php;
}
location ~* \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_split_path_info ^(.+\.php)(.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
I've been thinking all along this is a simple path issue b/c Codeigniter has a somewhat confusing routing structure but just can't see the problem with my config. Thoughts?
Everything up top matches what I have for a CI project, then this is my location config:
location ~ \.php$ {
include /etc/nginx/fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
Not sure if by removing the wildcard * would help, or getting rid of the split_info param...