How to use different php configuration on different nginx location directive? - php

I want to set a different php variable on different location directive.
But it is not working.
Here is the code:
server {
listen 1001;
listen [::]:1001;
server_name localhost;
error_log /home/ankurgupta/Desktop/mediaserver.error.log warn;
location /media {
root /home/xxxxxx/Server/websites/localhost/proxy;
index index.php;
try_files $uri $uri/ /media-php/index.php?$query_string;
location ~ \.php$ {
fastcgi_param WEBP "DISABLED";
include nginxconfig.io/php8.1_fastcgi.conf;
}
}
location /media-webp {
root /home/xxxxxx/Server/websites/localhost/proxy;
index index.php;
try_files $uri $uri/ /media-php/index.php?$query_string;
location ~ \.php$ {
fastcgi_param WEBP "ENABLED";
include nginxconfig.io/php8.1_fastcgi.conf;
}
}
}

Related

NGINX and Yii2 advance add another project in sub-directory

By usng NGINX and php-fpm, I want to add another Yii2 advance project under sub-directory like this
www.xyz.net - frontend project A, root at /var/www/html
www.xyz.net/admin - backend project A, root at /var/www/html
www.xyz.net/old - frontend project B, root at /var/www/old
www.xyz.net/old/admin - backend project B, root at /var/www/old
The below is the nginx configuration which I am using for 1 project
How can I modify it for adding another Yii2 advance project in sub-directory?
server {
server_name www.xyz.net;
listen 80;
set $base_root /var/www/html;
root $base_root;
charset utf-8;
index index.php index.html index.htm;
client_max_body_size 128M;
location / {
root $base_root/frontend/web;
try_files $uri $uri/ /frontend/web/index.php$is_args$args;
location ~ ^/assets/.+\.php(/|$) {
deny all;
}
}
location /admin {
alias $base_root/backend/web/;
location = /admin/ {
return 301 /admin;
}
location = /admin {
try_files $uri /backend/web/index.php$is_args$args;
}
try_files $uri $uri/ /backend/web/index.php$is_args$args;
location ~ ^/admin/assets/.+\.php(/|$) {
deny all;
}
}
location ~ ^/.+\.php(/|$) {
rewrite (?!^/((frontend|backend)/web|admin))^ /frontend/web$uri break;
rewrite (?!^/backend/web)^/admin(/.+)$ /backend/web$1 break;
fastcgi_pass php:9000;
fastcgi_split_path_info ^(.+\.php)(.*)$;
include /etc/nginx/fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
try_files $fastcgi_script_name =404;
}
location ~ /\. {
deny all;
}
}
you can add multiple server name to your config :
server {
listen 80;
server_name www.xyz.net;
set $base_root /var/www/html;
root $base_root;
...
}
server {
listen 80;
server_name www.xyz.admin.net/admin;
set $base_root /var/www/html/admin;
root $base_root;
...
}
and add www.xyz.admin.net and www.xyz.net to your /etc/hosts :
127.0.0.1 www.xyz.net
127.0.0.1 www.xyz.admin.net

NGINX config for php backend and JS frontend

I'm trying to serve my frontend app under /, but have requests for /oauth2 pass off to a php backend. Here is my latest nginx config attempt:
upstream dockerphp {
server backendphp:9000;
}
server {
listen 80;
server_name localhost;
index index.html;
root /application/frontend/build;
location /oauth2 {
root /application/public;
index index.php;
try_files $uri $uri/ /index.php$is_args$args;
#try_files /index.php$is_args$args =404;
location ~ \.php$ {
include /etc/nginx/fastcgi_params;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_pass dockerphp;
fastcgi_index index.php;
}
}
location / {
try_files $uri $uri/ /index.html;
}
}
I've tried just about every combination of config I can think of and just can't get it to work. Most of the time I end up with 404s.
Both my nginx and php docker containers have the same /application directory mounted.
With the above config, any requests to /oauth2/blah are being picked up by the location block at the bottom and therefore back to my frontend. This is probably my biggest problem - the /oauth2 location block to my mind is more "specific" so why isn't it "winning"?
I tried the commented out try_files line instead (to see whether index.php being the "fallback" value had an effect on specificity), and nginx just started downloading the index.php file rather than passing on the request. Help?
This is the approach that I use:
attempt to serve js / static pages first
if 1.) fails, pass to PHP backend
define a location for handling .php
upstream dockerphp {
server backendphp:9000;
}
server {
listen 80;
server_name localhost;
index index.html;
root /application/frontend/build;
location / {
try_files $uri $uri/ #php;
}
location #php {
root /application/public;
index index.php;
try_files $uri $document_root/index.php?$query_string;
# $document_root/index.php is the important part due to how root and alias directives work
}
location ~ \.php$ {
include /etc/nginx/fastcgi_params;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_pass dockerphp;
fastcgi_index index.php;
}
}
The location /oauth2 only wins when the URL you try is exactly website.com/oauth2. Add ^~ and the route will win all of the URLs starting with /oauth2, like this:
location ^~ /oauth2 {
For reference I eventually found a simple working solution (below).
upstream dockerphp {
server backendphp:9000;
}
server {
listen 80;
server_name localhost;
index index.html;
root /application/frontend/build;
location / {
try_files $uri $uri/ /index.html;
}
location /oauth2 {
try_files $uri $uri/ #php;
}
location #php {
include /etc/nginx/fastcgi_params;
fastcgi_pass dockerphp;
fastcgi_param SCRIPT_FILENAME /application/public/index.php;
}
}

nginx with a separate root per subdirectory

I'm trying to setup nginx so each path has it's own root directory. This is working for the most part, however POST to php-fpm a throwing a 405.
Currently trying:
location ^~ /foo {
alias /www/foo;
#index index.php;
try_files $uri /www/foo/index.php$request_uri;
access_log /var/log/nginx/foo.log main;
error_log /var/log/nginx/foo.log error;
}
location ^~ /bar {
alias /www/bar;
#index index.php;
try_files $uri /www2/bar/index.php$request_uri;
access_log /var/log/nginx/bar.log main;
error_log /var/log/nginx/bar.log error;
}
location ~ \.php {
set $php_root /usr/local/deploy/baz/current/web;
if ($request_uri ~* /foo ) {
set $php_root /www/foo/current/web;
}
if ($request_uri ~* /bar ) {
set $php_root /www2/bar/current/web;
}
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $php_root$fastcgi_script_name;
fastcgi_param DOCUMENT_ROOT $php_root;
include fastcgi_params;
}
Alias does not append location path to the file path. Check the logical flow. If server root is at /var/www/public, foo is located at /var/www/foo/public, bar is located at /var/www/bar/public. Then, this will be the easy config :
server {
root /var/www/public;
...
location /foo {
root /var/www/foo/public;
}
location ~ /foo/.+\.php$ {
fastcgi_param SCRIPT_FILENAME /var/www/foo/public$fastcgi_script_name;
# rest of fastcgi
}
location /bar {
root /var/www/bar/public;
}
location ~ /bar/.+\.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME/var/www/bar/public$fastcgi_script_name;
# rest of fastcgi
}
} # ends server
If you want to use PHP in one directive then :
server {
...
root /var/www/public;
...
location /foo {
root /var/www/foo/public;
}
location /bar {
root /var/www/bar/public;
}
location ~ \.php$ {
set $php_root /var/www/public;
if ($request_uri ~* /foo) {
set $php_root /var/www/foo/public;
}
if ($request_uri ~* /bar) {
set $php_root /var/www/bar/public;
}
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $php_root$fastcgi_script_name;
include /etc/nginx/fastcgi_params;
}
...
} # server block ends
Usage of alias -- http://nginx.org/en/docs/http/ngx_http_core_module.html#alias
Nginx modifier -- http://nginx.org/en/docs/http/ngx_http_core_module.html#location
If you need more number of path, then you have to symlink.

PHP, Nginx: Call {name}.php when url is sitename.com/{name}

Use nginx server.
What and how should be done when the URL is sitename.com/test and there is a call to test.php
For example:
project structure
public_html
|
+-somedir1
| |
| +-somefiles....
+-somdir2
|
+-login.php
+-info.php
+-something.php
......
when the url is sitename.com/login... call login.php, sitename.com/info call info.php etc.
server {
listen 80 default;
root /home/prod/frontend;
index index.php index.html index.htm;
charset utf-8;
server_name ui;
access_log /var/log/nginx/ui.access.log;
error_log /var/log/nginx/ui.error.log;
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
fastcgi_pass unix:/var/run/php7.0-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\. {
deny all;
}
}
also i try
location / {
try_files $uri.php $uri/ /index.php?$args;
}
location / {
index $uri.php
}

Nginx with angular and codeigniter applications in subfolders

I'm trying to make a website with codeigniter in the backend and angularjs in the frontend.
For the backend I'm using nginx as a server.
Currently I have problems with proper configuration of the nginx. What I want to achieve is to have codeigniter and angularjs applications in separate folders. And What I want is to access as follows
codeigniter from my_website.com/api
angular from my_website.com
And in terms of folder I want to keep them in:
codeigniter: /var/www/html/api
angular: /var/www/html/angular
So far I've manage to make it work from the same folder. So now I have codeigniter in /var/www/html and angular folder is in the same directory. And that way I can access codeigniter from my_website.com/ and angular from my_website.com/app which is not what I want.
I was trying multiple different setups for nginx /etc/nginx/sites-available/default file, but every single time with some problems. Farthest I've got was to have those in separate folders but codeigniter was working only for default_controller and I couldn't access any other files.
That's the current working config.
server {
listen 80;
listen [::]:80 default_server ipv6only=on;
root /var/www/html;
server_name localhost;
index index.php index.htm index.html;
location /app/ {
root /var/www/html/angular;
try_files $uri/ $uri index.html =404;
}
location / {
try_files $uri $uri/ /index.php;
#index index.html;
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/html$fastcgi_script_name;
include fastcgi_params;
}
}
And here is simple config where I just try to run codeigniter from demo sub-folder,
server {
listen 80;
server_name localhost;
root /var/www/html;
autoindex on;
index index.php;
location / {
#try_files $uri $uri/ /index.php;
try_files $uri $uri/ /index.php$request_uri;
location = /demo/index.php {
try_files $uri $uri/ /index.php$request_uri;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
fastcgi_param SCRIPT_FILENAME /var/www/html/demo$fastcgi_script_name;
include fastcgi_params;
}
}
location ~ \.php$ {
return 444;
}
}
And here my_website.com/demo/index.php works fine but my_website.com/demo/index.php/welcome shows 500 Internal Server Error
I hope I make myself clear.
I tried so many different configs found online, but I always had some troubles with that. Can any of you propose some simple solution for that problem?
Thanks,
Mateusz
I've finally managed to make it work the way I want. I've used this solution link to make it work with codeigniter in sub-directory which was the main problem. Here is my config:
server {
listen 80;
server_name localhost;
root /var/www/html;
autoindex on;
index index.html;
location / {
root /var/www/html/angular;
try_files $uri/ $uri index.html =404;
}
location /api/ {
alias /var/www/html/api/;
try_files $uri $uri/ /api/index.php;
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_index index.php;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
include /etc/nginx/fastcgi_params;
fastcgi_param SCRIPT_FILENAME $request_filename;
}
}
location ~ \.php$ {
return 444;
}
}
Thanks to #FrédéricHenri for suggesting using the log file under /var/log/nginx/*.log, that helped with tracking the way nginx is redirecting requests. Also what I've encounter was that web-browser was caching the website(I think that is what was happening) so that was giving me false results while I was making some changes to the config file.
If you have already set up ~ .php and other stuff. then Just need to add
location /ci-project {
try_files $uri $uri/ /ci-project/index.php;
}
In your server {} object of Nginx configuration

Categories