Configuring Nginx for multiple php sites on windows - php

I am a web developer and I build websites. But I have the following problem:
Since I can work from home (like probably most of us devs can do), I want to use nginx with php to continue working on websites. At work, one of my colleagues gave me one nginx configuration file which can serve multiple sites. I will post the whole code block below.
The problem is that I am working on windows 10 and I do not know how to make this code work on my machine.
My php version is PHP 7.3.12 which is located under C:\php.
My nginx version is 1.16.1 which is located under C:\nginx-1.16.1.
I appreciate every help I can get!
server {
index index.php index.html index.htm;
set $basepath "/home/nik/Code/";
set $domain $host;
set $dynroot "/var/www";
# check multi name domain to multi application
if ($domain ~ "^(.*)\.(.[^.]*)\.(site|vpnsite)$") {
set $customer $1;
set $developer $2;
set $rootpath "${developer}/${customer}/";
set $servername "${customer}.${developer}.site";
set $dynroot "$basepath/$rootpath";
}
if ($domain ~ "^(.*)\.(.[^.]*)\.laravel$") {
set $customer $1;
set $developer $2;
set $rootpath "${developer}/${customer}/";
set $servername "${customer}.${developer}.laravel";
set $dynroot "$basepath/$rootpath/public";
}
client_max_body_size 134M;
server_name "dev"
access_log "/home/nik/Code/App/nginx-logs/${servername}.access.log";
error_log "/home/nik/Code/App/nginx-logs/combined.error.log";
root $dynroot;
# check file exist and send request sting to index.php
location / {
try_files $uri $uri/ /index.php?$args;
autoindex on;
}
# allow execute all php files
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.2-fpm.sock;
}
# configure phpmyadmin path
location /phpmyadmin {
root /usr/share/;
index index.php;
location ~ ^/phpmyadmin/(.+\.php)$ {
try_files $uri =404;
root /usr/share/;
#fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_pass unix:/run/php/php7.2-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $request_filename;
include fastcgi_params;
fastcgi_param PATH_INFO $fastcgi_script_name;
fastcgi_buffer_size 128k;
fastcgi_buffers 256 4k;
fastcgi_busy_buffers_size 256k;
fastcgi_temp_file_write_size 256k;
fastcgi_intercept_errors on;
}
location ~* ^/phpmyadmin/(.+\.(jpg|jpeg|gif|css|png|js|ico|html|xml|txt))$ {
root /usr/share/;
}
}
location /phpMyAdmin {
rewrite ^/* /phpmyadmin last;
}
# disallow access to apache configs
location ~ /\.ht {
deny all;
}
# disallow access to git configs path
location ~ /\.git {
deny all;
}
}

Related

NGINX rewrite configuration for another website in a sub directory triggers script download

This is my original nginx configuration
server {
listen 80;
server_name my-site.com;
return 301 http://www.my-site.com$request_uri;
}
server {
listen 80;
server_name www.my-site.com;
root /var/www/sites/mysite/public_html;
index index.php index.html index.html;
access_log /var/log/nginx/mysite.access.log;
error_log /var/log/nginx/mysite.error.log;
ssl off;
location / {
try_files $uri $uri/ index.php;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/var/run/php-fpm.sock;
fastcgi_read_timeout 180;
fastcgi_buffers 16 16k;
fastcgi_buffer_size 32k;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
Then I want to add another site in subfolder lets say "my-site.com/another_app" but this site requires rewriting url like:
my-site.com/another_app/api
my-site.com/another_app/home
becomes
my-site.com/another-app/api.php
my-site.com/another-app/index.php?mod=home
i already tried to add rewrite inside the "location /" like this
location / {
try_files $uri $uri/ index.php;
rewrite ^/another-app/api$ /another-app/api.php break;
rewrite ^/another-app/([A-Za-z0-9-|_]+)/?$ /another-app/index.php?mod=$1 break;
}
But every time I try to access "my-site.com/another-app/api" it triggers to download the php script
Did I missing something?
I can't comment yet so I answer this way.
I have had this problem only with static files like css and js:
I fixed it with indexing the files in the static folder:
location /static/ {
# My static files including the css reside inside my static
# folder e.g. /static/css/style.css
# autoindex on will index everything under the given location
autoindex on;
}
Also what #Richard Smith said, try using last.

aws + centos 7 + built nginx ngx_pagespeed + php-fpm = file not found

I have tried everything and have verified everything is running on the same group.
Also chown'd files to nginx:nginx and chmod'd them to 755
Here is server block
server {
listen 80;
root /var/www/example.com/public_html;
index index.php index.html index.htm;
server_name example.com;
location / {
try_files $uri $uri/ /index.php?q=$uri&$args;
}
# Pagespeed main settings
pagespeed on;
pagespeed FileCachePath /var/ngx_pagespeed_cache;
# Ensure requests for pagespeed optimized resources go to the pagespeed
# handler and no extraneous headers get set.
location ~ "\.pagespeed\.([a-z]\.)?[a-z]{2}\.[^.]{10}\.[^.]+" { add_header "" ""; }
location ~ "^/ngx_pagespeed_static/" { }
location ~ "^/ngx_pagespeed_beacon" { }
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
include fastcgi_params;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_intercept_errors on;
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
}
}
Followed this tutorial about SELinux. I am new to CentOS so I was unfamiliar with all of the security parameters.
https://www.cloudinsidr.com/content/troubleshooting-php-7-tcp-sockets-with-selinux-on-centos-7-rhelfedora/

How to properly configure alias directive in nginx?

I have been trying to configure multiple webapp on my nginx webserver but I can't get working one Laravel app that requires $document_root set to laravel public folder.
I am currently trying to configure it using alias directive but for an obscure reason this doesn't work. Here is what I am trying to do.
# Default server configuration
#
server {
listen 80;
# SSL configuration
#
listen 443 ssl;
error_log /var/log/nginx/error.log warn;
ssl_certificate /etc/nginx/ssl/server.crt;
ssl_certificate_key /etc/nginx/ssl/server.key;
set $root_path '/var/www/html';
root $root_path;
# Add index.php to the list if you are using PHP
index index.html index.htm index.nginx-debian.html index.php;
server_name localhost;
location /paperwork {
alias /var/www/html/paperwork/frontend/public;
try_files $uri $uri/;
#location ~ \.php {
# fastcgi_split_path_info ^(.+\.php)(.*)$;
# fastcgi_pass unix:/var/run/php5-fpm.sock;
# include /etc/nginx/fastcgi_params;
# #fastcgi_param SCRIPT_FILENAME /var/www/html$fastcgi_script_name;
# #fastcgi_intercept_errors on;
#}
}
#location #paperwork {
# rewrite /paperwork/(.*)$ /paperwork/index.php/$1 last;
#}
location / {
}
location /wallabag {
try_files $uri $uri/ /index.php;
}
location /laverna {
try_files $uri/ /index.php;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
# With php5-cgi alone:
#fastcgi_pass 127.0.0.1:9000;
# With php5-fpm:
fastcgi_split_path_info ^(.+\.php)(/.+)$;
#try_files $uri $uri/ =404;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
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;
}
}
To test my "alias" config I put a 'test.php' files in /var/www/html/paperwork/frontend/public/test.php and tried to access it via https://IP/paperwork/test.php. I get a 404 error and nothing in nginx error log.
If I try https://IP/paperwork/frontend/public/test.php in browser it displays the test.php file without errors.
Nothing change if I uncomment try_files line in php location.
If I copy test.php to /var/www/html/paperwork/test2.php and access to https://IP/paperwork/test2.php the file is displayed without errors so I can see here that alias is not working as there is not a test2.php in paperwork public directory.
I can have a different behaviour if I uncomment php location inside paperwork location. With this, requests like https://IP/paperwork/test.php do not display a 404 but a blank screen.
I have been through a lot of forums / questions related to this but I couldn't get a working config for a simple task like displaying test.php...
Thanks !
I found the solution. It seems that a wrong request was sent for php files. When alias is used it is recommend to use $request_filename instead of $fastcgi_script_name.
Here is my location block :
location /paperwork {
alias /var/www/html/paperwork/frontend/public;
#try_files $uri $uri/;
location ~ \.php$ {
fastcgi_pass unix:/var/run/php5-fpm.sock;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $request_filename;
#fastcgi_intercept_errors on;
}
}
This solved my problem for my 'test.php' file which is now executed while reaching https://IP/paperwork/test.php. So alias is working and php is well executed.
I still have a problem when trying to reach 'index.php' (which is my laravel app index). File is found but instead of executing it is downloaded. So when I reach https://IP/paperwork/index.php I get a login file downloaded which is index.php file. I get same behaviour if I try /paperwork/index.php/login or /paperwork/login.
try this:
location /api/ {
index index.php index.html index.htm;
alias /app/www/;
location ~* "\.php$" {
try_files $uri =404;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $request_filename;
}
}
https://www.nginx.com/resources/wiki/start/topics/tutorials/config_pitfalls/

How to correctly configure Nginx for PHP (Yii framework and Zurmo)

I am trying to setup Zurmo CRM on my local machine (Win8x64). After installing all the requirements I want to get started with the actual installation. The problem is that it seems the paths are not correctly passed from NGinx to FastCGI PHP. Here is my Nginx serve configuration:
server {
listen 80;
server_name zurmo.local;
root html/zurmo.local;
set $index "index.php";
charset utf-8;
location / {
index index.html $index;
try_files $uri $uri/ /$index?$args;
}
location ~ ^/(protected|framework|themes/\w+/views) {
deny all;
}
location ~ \.(js|css|png|jpg|gif|swf|ico|pdf|mov|fla|zip|rar)$ {
try_files $uri =404;
}
location ~ \.php {
fastcgi_split_path_info ^(.+\.php)(.*)$;
set $fsn /$index;
if (-f $document_root$fastcgi_script_name){
set $fsn $fastcgi_script_name;
}
fastcgi_pass 127.0.0.1:9000;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fsn;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fsn;
}
location ~ /\.ht {
deny all;
}
}
As a result, when I make a call to zurmo.local (which is added to hosts file) i get "This webpage has a redirect loop" with a URI that looks like this http://zurmo.local/app/app/ [...] /app/app/index.php If instead of $document_root$fsn and I comment the PATH_INFO and PATH_TRANSLATED than I get No input file specified. with a URI that looks like http://zurmo.local/app/app/index.php
Looking further into it, when I have added access_log html/zurmo.local/logs/access.log; the Nginx error.log shows me the following: [timestamp] [emerg] 4064#3660: CreateFile() "[path to stack]\nginx/html/zurmo.local/logs/access.log" failed (3: The system cannot find the path specified). As you can see the directory separator is not consistent.
One last note, my Nginx home directory is situated at nginx/html which is in fact a smlink to of ../home This is purely for keeping my file structure in a way that fits my day to day work.
How can I correctly configure Nginx in order to proceed (with the Zurmo installation) ?
I know this is an old question, but here is what I have done to make nginx + zurmo work.
server {
listen 80;
server_name zurmo.local;
root /home/www/zurmo.local;
access_log /var/log/nginx/zurmo.access.log main;
index index.php;
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ ^/(protected|framework|themes/\w+/views) { deny all; }
location ~ /\. { deny all; access_log off; log_not_found off; }
location = /favicon.ico { log_not_found off; access_log off; }
location ~ \.(js|css|png|jpg|gif|ico|pdf|zip|rar)$ {
try_files $uri =404;
}
location ~ \.php {
fastcgi_split_path_info ^(.+\.php)(.*)$;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_read_timeout 600s;
fastcgi_send_timeout 600s;
}
}
I don't think you need the if() statement in your *.php block. In my nginx setups that's all i ever needed:
# Process PHP files
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
# Include the standard fastcgi_params file included with nginx
include fastcgi_params;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass 127.0.0.1:9000;
}

Nginx 404 not found in subdir

Okay, i setup a debian install with ispconfig3 ,php5-fpm and nginx, the main site, drupal is working fine, even perfect. But i also have a modded phpbb3 board, in a subdir, named 'forum'.
Now for some reason i get the following error from nginx when accesing the dir:
'ERROR 500 - Internal Server Error!'
This is the content of the nginx directiv field(for vhost):
# search for already compressed files
gzip_static on;
gzip on;
# some images have no mime type
default_type image/jpeg;
# 404 generated from php can be rather slow. Uncomment with care
error_page 404 /index.php;
# disallow access to version control directory, but return 404, not to disclose information
location /.git {
return 404;
}
# robots.txt is important for search engines
location /robots.txt {
access_log off;
}
# This is mostly based on Drupal's stock .htaccess
location ~* ^.+(\.(txt|engine|inc|info|install|module|profile|po|sh|.*sql|theme|tpl(\.php)?|xtmpl)|code-style\.pl|/Entries.*|/Repository|/Root|/Tag|/Template)$ {
return 404;
}
# serve imagecache files directly or redirect to drupal if they do not exist
location ~* imagecache {
access_log off;
expires 30d;
try_files $uri #drupal;
}
# Drupal 7 image stylef
location ~* image/generate {
access_log off;
expires 30d;
try_files $uri #drupal;
}
# serve static files directly
location ~* ^.+\.(jpg|jpeg|gif|css|png|js|ico|html|xml|swf|flv)$ {
access_log off;
log_not_found off;
expires 30d;
}
location #rewrite {
# Some modules enforce no slash (/) at the end of the URL
# Else this rewrite block wouldn't be needed (GlobalRedirect)
rewrite ^/(.*)$ /index.php?q=$1;
break;
}
# This rewrites pages to be sent to PHP processing
location #drupal {
rewrite ^/(.*)$ /index.php?q=$1 last;
}
location / {
try_files $uri #cache;
}
# This will try to see if we have a boost file in place. no harm done if this is not used
location #cache {
# queries, drupal cookies, or not GET methods, all require PHP processing.
if ($query_string ~ ".+") {
return 405;
}
if ($http_cookie ~ "DRUPAL_UID" ) {
return 405;
}
if ($request_method !~ ^(GET|HEAD)$ ) {
return 405;
}
error_page 405 = #drupal;
# Drupal uses 1978 - I am 4 years older than Dries :)
add_header Expires "Tue, 22 Sep 1974 08:00:00 GMT";
add_header Cache-Control "must-revalidate, post-check=0, pre-check=0";
try_files /cache/normal/$host/${uri}_.html /cache/mobile/$host/${uri}_.html /cache/perm/$host/${uri}_.css /cache/perm/$host/${uri}_.js /cache/$host/0$uri.html /cache/$host/0${uri}/index.html #drupal;
}
location /phpmyadmin {
root /usr/share/;
index index.php index.html index.htm;
location ~ ^/phpmyadmin/(.+\.php)$ {
try_files $uri =404;
root /usr/share/;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $request_filename;
include /etc/nginx/fastcgi_params;
fastcgi_param PATH_INFO $fastcgi_script_name;
fastcgi_buffer_size 128k;
fastcgi_buffers 256 4k;
fastcgi_busy_buffers_size 256k;
fastcgi_temp_file_write_size 256k;
fastcgi_intercept_errors on;
}
location ~* ^/phpmyadmin/(.+\.(jpg|jpeg|gif|css|png|js|ico|html|xml|txt))$ {
root /usr/share/;
}
}
location /phpMyAdmin {
rewrite ^/* /phpmyadmin last;
}
location /forum {
root /var/www/ashladan.net/web;
index index.php index.html index.htm;
location ~ ^/forum/(.+\.php)$ {
try_files $uri =404;
root /var/www/ashladan.net/web;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $request_filename;
include /etc/nginx/fastcgi_params;
fastcgi_param PATH_INFO $fastcgi_script_name;
fastcgi_buffer_size 128k;
fastcgi_buffers 256 4k;
fastcgi_busy_buffers_size 256k;
fastcgi_temp_file_write_size 256k;
fastcgi_intercept_errors on;
}
location ~* ^/forum/(.+\.(jpg|jpeg|gif|css|png|js|ico|html|xml|txt))$ {
root /var/www/ashladan.net/web;
}
}
It may be standard PHP response for uncaught exception / fatal / syntax error with no output.
You can either turn on error logging in php.ini and check your log file, or just at the beginning of your index.php file in this directory add two lines:
<?php
error_reporting(E_ALL);
ini_set('display_errors','on');
So if it is PHP's fault you will get more details on this error.

Categories