NGINX and Yii2 advance add another project in sub-directory - php

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

Related

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

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

nginx conf for dockerized wordpress, fastapi, nginx on the local machine

I want to setup php (wordpress) and a backend API (fastapi) on the same local machine for development. I am using docker swarm mode on this single node. So, nginx, php, and backend are all docker services.
When I use following nginx.conf, wordpress takes over the whole address space. E.g. when I write localhost/api/docs, wordpress shows me its main page. Whereas I would like to see my fastapi documentation. How can I fix this?
upstream php {
server unix:/tmp/php-cgi.socket;
server php:9000;
}
server {
listen 80;
server_name _;
location /api {
proxy_pass http://backend:8888/api;
}
location / {
root /var/www/html;
index index.php index.html;
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME /var/www/html$fastcgi_script_name;
fastcgi_intercept_errors on;
if ($uri !~ "^/images/") {
fastcgi_pass php;
}
}
location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
root /var/www/html;
expires max;
log_not_found off;
}
}

NGINX access multiple sites same IP url

I would like to know how I can have several sites on Nginx and be able to access each of them with the same IP (without the domain, since I am doing tests in a laboratory locally).
I have the server on a separate PC and I access it remotely from my computer using the IP. Both are on the same LAN.
In the directory /var/www/ I have two sites 'nextcloud' and 'phpmyadmin'. I would like to be able to enter both by placing (for example) 192.168.1.14/nextcloud and 192.168.1.14/phpmyadmin. Or having any other project in the www directory.
I tried all the solutions I found, but none of them worked for me. When I enter phpmyadmin for example, it gives me to download the page instead of entering it.
Within /etc/nginx/sites-enabled I have the two files, one from nextcloud:
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/nextcloud/;
index index.php index.html index.htm;
server_name localhost;
client_max_body_size 512M;
fastcgi_buffers 64 4K;
location / {
root /var/www/nextcloud;
rewrite ^ /index.php$request_uri;
}
location ~ ^/(?:build|tests|config|lib|3rdparty|templates|data)/ {
deny all;
}
location ~ ^/(?:\.|autotest|occ|issue|indie|db_|console) {
deny all;
}
location ~ ^/(?:index|remote|public|cron|core/ajax/update|status|ocs/v[12]|updater/.+|ocs-provider/.+)\.php(?:$|/) {
fastcgi_split_path_info ^(.+?\.php)(/.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
fastcgi_intercept_errors on;
fastcgi_request_buffering off;
}
location ~ ^/(?:updater|ocs-provider)(?:$|/) {
try_files $uri/ =404;
index index.php;
}
location ~ \.(?:css|js|woff|svg|gif)$ {
try_files $uri /index.php$request_uri;
add_header Cache-Control "public, max-age=15778463";
access_log off;
}
location ~ \.(?:png|html|ttf|ico|jpg|jpeg)$ {
try_files $uri /index.php$request_uri;
# Optional: Don't log access to other assets
access_log off;
}
}
And that of phpmyadmin:
server {
listen 80;
listen [::]:80;
root /var/www/phpmyadmin/;
# Add index.php to the list if you are using PHP
index index.php index.html index.htm index.nginx-debian.html;
server_name localhost;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
}
# pass PHP scripts to FastCGI server
#
location ~ \.php$ {
include snippets/fastcgi-php.conf;
#
# # With php-fpm (or other unix sockets):
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
# # With php-cgi (or other tcp sockets):
# fastcgi_pass 127.0.0.1:9000;
}
}
Try creating two test folders in /var/www/ (test1 and test2), each with an index.html file inside and modifying the nginx default file, but it didn't work for me either
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name localhost;
index index.html;
location / {
return 410; # Default root of site won't exist.
}
location /test1/ {
alias /var/www/test1/;
try_files $uri $uri/ =404;
# any additional configuration for non-static content
}
location /test2/ {
alias /var/www/test2/;
try_files $uri $uri/ =404;
# any additional configuration for non-static content
}
}
As I said, I tried different solutions. Another problem I had was that it only redirected me to nextcloud, although I put phpmyadmin in the url. And the previous one that I already mentioned, that when I enter, download the index.php. Thank you.
Sorry for my English.
Simple add nextcloud.my and phpmyadmin.my to your .hosts file and listen domain name in Nginx.
The option that you proposed can also be made to work, but it is full of bugs and difficulties can occur during the transfer to work server.

PHP app in subdirectory in Nginx

I'm using Ubuntu 16.04, Nginx 1.10.3 and PHP 7.0. The example PHP applications are CodeIgniter 3.1.5.
I am trying to run a static page at the root of my site www.example.com(works) with multiple other CodeIgniter applications each running in subdirectories at www.example.com/client-a, www.example.com/client-b, etc.
The static page at my root runs fine, however when redirecting to the apps at the sub directories none of the stylesheets and scripts get loaded resulting in 404 errors. The routing works though.
The application files of the subsequent apps don't exist within one another.The application root exists in /var/www/example/public_html while the "nested" applications lives in /var/www/client_a/public_html, /var/www/client_b/public_html etc.
Here is my Nginx server block:
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name example.com www.example.com;
root /var/www/example/public_html;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
location /client-a {
alias /var/www/client_a/public_html;
try_files $uri $uri/ #nested;
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_param SCRIPT_FILENAME $request_filename;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
}
}
location #nested {
rewrite /client_a/(.*)$ /client_a/index.php?/$1 last;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}
location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
expires 30d;
}
location ~ /\.ht {
deny all;
}
}

laravel nginx different roots to different projects

I have global project (web wrapp) and multiple projects. Structure of files
project/
application/
index.php
project1/
project2(on laravel)/
public/
index.php
So, if i call project, i get index.php.
I want to run project2/public/index.php with url /project2.
nginx conf below:
server {
listen 80 default_server;
listen [::]:80 default_server;
root /home/user/projects/project/application;
index index.php index.html index.htm;
location /project2/ {
root /home/user/projects/project/application/project2/public/;
try_files $uri $uri/ /index.php$is_args$args;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.1-fpm.sock;
fastcgi_read_timeout 600;
}
}
With this configuration I get nginx's 404 when ask /project2. If I replase
location /project2/ {
root /home/user/projects/project/application/project2/public/;
try_files $uri $uri/ /index.php$is_args$args;
}
with
location /project2/ {
root /home/user/projects/project/application/project2/public;
try_files $uri $uri/ /project2/public/index.php$is_args$args;
}
I get Not found from Laravel. Is it possible to solve my problem?
You shouldn't need to set the root inside the project2 location:
Try this:
server {
listen 80 default_server;
listen [::]:80 default_server;
root /home/user/projects/project/application;
index index.php index.html index.htm;
location ~ /project2 {
try_files /project2/public/$uri /project2/public/$uri/ /project2/public/index.php?q=$uri&$args;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.1-fpm.sock;
fastcgi_read_timeout 600;
}
}
I thought, in this point problem was in laravel. I put all my routes in Route::prefix('project2', function(){ ... }); and it worked. I used
location /project2/ {
root /home/user/projects/project/application/project2/public;
try_files $uri $uri/ /project2/public/index.php$is_args$args;
}

Categories