Nginx multiple domain directives - php

I'm using Nginx for a PHP project. Here is what I do in /etc/nginx/sites-available/default:
server {
server_name domain_a.com;
include /etc/nginx/main.conf; // listen, php directives, etc.
location ~ (.*)\.php(/|$) {
fastcgi_pass php:9000;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param HTTPS off;
}
}
server {
server_name domain_b.com;
include /etc/nginx/main.conf;
location ~ (.*)\.php(/|$) {
fastcgi_pass php:9000;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param HTTPS off;
// Specific to this domain
auth_basic "Authentication";
auth_basic_user_file /etc/nginx/.htpasswd;
}
}
/etc/nginx/main.conf
listen 80;
client_max_body_size 5M;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
root /var/www/web;
I have a lot of repetition in this code : The two location block could be merge in one for the two domains. I think I could use a if statement to add my specific code for domain_b but it's cleary not a recommanded way according to the documentation http://wiki.nginx.org/IfIsEvil
Do you have idea how I can do to respect the DRY concept ?
Thx,

For me I create a folder in my conf and call it includes for example and then you can include this where ever you want, for example
# /etc/nginx/includes/php.conf
location ~ (.*)\.php(/|$) {
fastcgi_pass php:9000;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param HTTPS off;
}
Then in your configurations do something like this
server {
server_name domain_a.com;
include /etc/nginx/main.conf; // listen, php directives, etc.
include /etc/nginx/includes/php.conf;
}
server {
server_name domain_b.com;
include /etc/nginx/main.conf;
include /etc/nginx/includes/php.conf;
auth_basic "Authentication";
auth_basic_user_file /etc/nginx/.htpasswd;
}
}

Related

Cannot load a php page with nginx - 502 Bad gateway

I have PHP 7.2.17 and Nginx 1.15.9 on Ubuntu 19.04.
I have this nginx configuration :
server {
listen 80;
listen [::]:80;
server_name dev.frameworkcms.com;
set $rootpath "/var/www/html/perso/framework-cms test";
error_log "/var/www/html/perso/framework-cms test/logs/error.log" error;
# combined by default
access_log "/var/www/html/perso/framework-cms test/logs/access.log";
root $rootpath/web;
location ~ \.php$ {
fastcgi_intercept_errors on;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_index Resources.php;
#fastcgi_pass 127.0.0.1:9000;
fastcgi_pass unix:/var/run/php7.2-fpm.sock;
}
}
I tried with the port 9000 as well.
i can see the index.html at the project root but not the file web/Resources.php.
I always have a 502 Bad Gateway. Logs are empty.
Edit
I just added a line try_files \$uri /Resources.php; at the beginning of the location block. Now the php file is offered for download...
Edit
Thanks to #Pete Cooper, I have found that the .sock file is not the good one.
Here is the new version of the file :
server {
listen 80;
listen [::]:80;
server_name dev.frameworkcms.com;
set $rootpath "/var/www/html/perso/framework-cms test";
error_log "/var/www/html/perso/framework-cms test/logs/error.log" error;
# combined by default
access_log "/var/www/html/perso/framework-cms test/logs/access.log";
root $rootpath/web;
location ~ \.php$ {
try_files \$uri /Resources.php;
fastcgi_intercept_errors on;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_index Resources.php;
#fastcgi_pass 127.0.0.1:9000;
fastcgi_pass unix:/run/php/php7.2-fpm.sock;
}
}
I now have a 500 Internal Server Error.
I precise that I work locally.
Try restarting your php-fpm service.

When should I use php-fpm, php-cli, php-mbstring with phpmyadmin/nginx config

After following many tutorials on nginx for a basic phpymadmin setup, I have concluded that I am confused as to why certain tutorials recommend certain php modules while others do not.
My working configuration is with php7-fpm, php7-mbstring.
server {
listen 80;
listen [::]:80;
server_name name ip;
root /var/www/html/phpmyadmin;
index index.php;
location / {
try_files $uri $uri/ =404;
}
location ~ ^/(.*\.php)$ {
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
fastcgi_param SCRIPT_FILENAME $request_filename;
include /etc/nginx/fastcgi_params;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_index index.php;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include /etc/nginx/fastcgi_params;
}
location ~ /\.ht {
deny all;
}
}
Most tutorials suggest php-fpm, php-cli, and another module.
Do I need these, and why would I need these modules for phpmyadmin?
Is my nginx configuration the correct way to have it function properly (not the most secure), or should I add any other locations/modules to my nginx configuration such as,
location ~* ^/phpmyadmin/(.+\.(jpg|jpeg|gif|css|png|js|ico|html|xml|txt))$ {
root /path/;

Nginx 405 not allowed to specific '.php'-file

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.

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;
}

Magento on Nginx - Configuration [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I'm working on an nginx.conf for running Magento, the site mostly works, magento is run using php-fpm.
But some parts of it are still not working, and I've tried every wiki, blog, etc around the web.
My problem is that where ever I have a Javascript pop-up on CMS pages and blocks, mainly the tiny_mce WYSIWYG editor, (/js/tiny_mce/plugins/advimage/image.htm etc) they open a page not found.
I don't know what should I do so this editor displays correctly.
Also, the downloader doesn't display.
It seems that each of these use its own index.php inside a different folder than root, so should I change the index to that?
like $document_root/downloader/index.php ?
I HIGHLY suggest you read and follow the nginx primer by Martin Fjordvald.
I use the following configuration for Magento. It not only works great, it also turns off access_log for images, etc. and has a special php-fpm configuration. Please note that the server root is specified within the server block. Several configuration files incorrectly specify it within a location block.
Magento nginx configuration file:
(Be sure to replace all paths and domain names accordingly)
server {
listen 80;
#listen 443 default ssl;
server_name DOMAIN.COM;
#rewrite requests to www
rewrite ^ $scheme://www.DOMAIN.COM$request_uri permanent;
}
server {
listen 80;
#listen 443 default ssl;
#ssl_certificate /path/to/ssl.crt;
#ssl_certificate_key /path/to/ssl.key;
server_name www.DOMAIN.COM;
# most likely /var/www/...
root /path/to/files;
include /etc/nginx/restrictions.conf;
location / {
index index.php;
if ($request_uri ~* "\.(ico|css|js|gif|jpe?g|png)$") {
access_log off;
expires max;
}
try_files $uri $uri/ #handler;
}
# protect directories
location /app/ {
deny all;
}
location /includes/ {
deny all;
}
location /lib/ {
deny all;
}
location /lib/minify/ {
allow all;
}
location /media/downloadable/ {
deny all;
}
location /pkginfo/ {
deny all;
}
location /report/config.xml {
deny all;
}
location /var/ {
deny all;
}
location /var/export/ {
# restrict access to admins
auth_basic "Restricted";
auth_basic_user_file htpasswd;
autoindex on;
}
location #handler {
rewrite ^(.*) /index.php?$1 last;
}
# include php specific configuration
include /etc/nginx/php.conf;
}
This is a php-fpm specific configuration file which intercepts error codes and splits the path info correctly so you have access to the correct path parts in PHP. I also use a Unix socket rather than a port due to performance improvements. Also note that you don't need to repeat the fastcgi_params already specified in fastcgi_params.
fastcgi_intercept_errors on;
# this will allow Nginx to intercept 4xx/5xx error codes
# Nginx will only intercept if there are error page rules defined
# -- This is better placed in the http {} block as a default
# -- in that virtual host's server block
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
# A handy function that became available in 0.7.31 that breaks down
# The path information based on the provided regex expression
# This is handy for requests such as file.php/some/paths/here/
include fastcgi_params;
fastcgi_index index.php;
fastcgi_pass unix:/var/run/phpfpm.sock;
}
My fastcgi_params configuration file is optimized for a small server (<1GB RAM). Be sure to adjust yours according to your server's performance:
fastcgi_param QUERY_STRING $query_string;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param REQUEST_URI $request_uri;
fastcgi_param DOCUMENT_URI $document_uri;
fastcgi_param DOCUMENT_ROOT $document_root;
fastcgi_param SERVER_PROTOCOL $server_protocol;
fastcgi_param GATEWAY_INTERFACE CGI/1.1;
fastcgi_param SERVER_SOFTWARE nginx/$nginx_version;
fastcgi_param REMOTE_ADDR $remote_addr;
fastcgi_param REMOTE_PORT $remote_port;
fastcgi_param SERVER_ADDR $server_addr;
fastcgi_param SERVER_PORT $server_port;
fastcgi_param SERVER_NAME $server_name;
# PHP only, required if PHP was built with --enable-force-cgi-redirect
fastcgi_param REDIRECT_STATUS 200;
fastcgi_connect_timeout 90;
fastcgi_send_timeout 180;
fastcgi_read_timeout 360;
fastcgi_buffer_size 1024k;
fastcgi_buffers 8 512k;
fastcgi_busy_buffers_size 1024k;
fastcgi_temp_file_write_size 1024k;
fastcgi_intercept_errors on;
fastcgi_pass_header *;
We have magento installed to mydomain.com/store, and we use next config for nginx:
server {
listen <needed ip(s)>:80;
server_name mydomain.com;
root /www/mydomain;
location ~ /\. {
deny all;
}
location ~* ^.+.(jpg|jpeg|gif|css|png|js|ico)$ {
access_log off;
expires 30d;
}
location /store/ {
fastcgi_pass 127.0.0.1:9000;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root/store/index.php;
fastcgi_param SCRIPT_NAME /store/index.php;
fastcgi_index index.php;
}
location /store/static/ { }
location /store/skin/ { }
location /store/media/ { }
location /store/errors/ { }
location ~* /store/errors/.*\.xml$ { deny all; }
location ~* /store/errors/.*\.phtml$ { deny all; }
location ~* /store/errors/.*\.php$ {
fastcgi_pass 127.0.0.1:9000;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME /store/errors$fastcgi_script_name;
fastcgi_index index.php;
fastcgi_read_timeout 600;
}
location /store/js/ { }
location ~* /store/js/.*\.php$ {
fastcgi_pass 127.0.0.1:9000;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root/store/js$fastcgi_script_name;
fastcgi_param SCRIPT_NAME /store/js$fastcgi_script_name;
fastcgi_index index.php;
fastcgi_read_timeout 600;
}
}
you have to rewrite all .htaccess rules to ngnix configuration to get this working. Worth to read http://www.nbs-system.co.uk/blog-2/magento-optimization-howto-en.html

Categories