Yii2 adanced application and nginx configuration (index directive configuration) - nginx

I'm trying to build proper server configuration for nginx serving Yii2 advanced template where backend hosted in a subfolder inside same domain name as frontend.
In this case path_to_yii2 contains whole Yii2 application template and we have these requirements:
path_to_yii2/frontend/web -> should be webroot of / location
path_to_yii2/backend/web -> should be webroot of /backend location
Static content in both folders should be properly served. PHP files should work in both cases.
I wrote and tested such configuration:
server {
listen 80;
server_name localhost;
root <path_to_yii2_application>;
location ~* ^/backend/(.+)$ {
try_files /backend/web/$1 /backend/web/$1/ /backend/index.php?$args;
index /backend/$1/index.php; # not working in case of exact /backend/ request
location ~* ^/backend/(.+\.php)$ {
#fastcgi_pass 127.0.0.1:9000;
fastcgi_pass unix:/var/run/php5-fpm.sock;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root/backend/web/$1;
}
}
location / {
try_files /frontend/web/$uri /index.php?$args;
index /$uri/index.php; # not working at all, but / location is served by php even without this line
}
location ~ \.php$ {
#fastcgi_pass 127.0.0.1:9000;
fastcgi_pass unix:/var/run/php5-fpm.sock;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root/frontend/web/$fastcgi_script_name;
}
}
And I have some unresolved problems with such configuration. I tested six different options:
FRONTEND:
Static content under / location should be served directly from frontend/web subfolder of yii2 application folder.
Nonexistent content here should be redirected to frontend/web/index.php?$args and served using ~ .php$ location with fastcgi.
Directories under / location should return indexes, if needed served with ~ .php$ location and fastcgi.
BACKEND:
Static content under /backend location should be served directly from backend/web subfolder of yii2 application folder.
Nonexistent content here should be redirected to backend/web/index.php?$argsand served using ~ .php$ location with fastcgi.
Directories under /backend location should return their indexes, if needed served with ~ .php$ location and fastcgi.
I have troubles with directories and their indexes (3 and 6).
First of all, directories for frontend section not working at all, seems that index /$uri/index.php; is wrong for some reason.
Secondly, directories for backend working except exact /backend/ url. Nginx doesn't serve index directive in =/backend/ case.
As a temporarily workaround for backend I added few lines for this exact url:
location = /backend {
return 301 https://$server_name/backend/index.php;
}
location = /backend/ {
return 301 https://$server_name/backend/index.php;
}
How to fix these indexes correctly and what I'm doing wrong?
P.S. There are some similar questions, like Migrating Yii2 from Apache to Nginx - failed on backend app (doesn't provide correct answer, recommends using subdomain) and Nginnx config for Yii 2 Advanced App Template (suggested to move backend content inside yii2 application to frontend folder). I believe that nginx configuration is a proper way of congiguring yii2-application template.
There is also https://github.com/mickgeek/yii2-advanced-one-domain-config repositary which not works in nginx > 1.8.1.
Interesting that apache just needs a symbolic link to make this work. Nginx before 1.8.1 too.
Yii2 application template can be git cloned from here: https://github.com/yiisoft/yii2-app-advanced.git

Related

Can not access prestashop admin menu except dashboard

I installed prestashop in my localhost. I can login to admin and saw the dashboard. But when I went to other menu, it said 404 not found. The problem was in dashboard, it is using url like index.php?controller , but in other menu it is using admin/index.php. I installed the software under ps directory.
OK - http://localhost/ps/admin/index.php?controller=AdminDashboard&token=3fca2bcd5f31ce3c1cdf951bf5620720#/preview
FAIL - http://localhost/ps/admin/index.php/sell/catalog/products?_token=IIPIHFzRMTdRMvjXGeCiFocCWVXBiwUhWgJIAhgzvtA
Here is my nginx default site configuration inside server {}
location /ps {
root /var/www/;
index index.php;
#try_files $uri $uri/ =404;
try_files $uri $uri/ /index.php?$args;
location ~ /ps/(.+\.php)$ {
fastcgi_pass unix:/run/php/php7.4-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
include snippets/fastcgi-php.conf;
}
}
I am using nginx version 1.18.0 and prestashop version 1.7.8.3 on ubuntu 20.04.4.
My question is, how to fix the nginx setting especially the try_files part so that I can access prestashop's other menu? If that is not possible, how to disable pretty url in prestashop?
Prestashop comes with built-in Apache rewriting rules,
so using a NGINX only enviroment could be troublesome.
You preferably have to switch to Apache as-persystem requirements :
https://devdocs.prestashop.com/1.7/basics/installation/system-requirements/
or consider using Nginx as a reverse proxy for static resources and Apache to serve PHP requests, so native htaccess will work out of the box.
Anyway , have a look at Nginx-specific Prestashop rules:
https://devdocs.prestashop.com/1.7/basics/installation/nginx/
to be integrated in your conf file.
While, in order to completely disable URL rewriting,
you can act on a backoffice setting "URL rewriting" in SEO&URL part, if you are not able to reach that page, you can just adjust "PS_REWRITING_SETTINGS" to 0 in ps_configuration table in your database.
I'm not sure if this will work with the backoffice routes that are now based on Symfony framework, though.

Nginx trying to load image files from server root rather then project root location

Quick question, shouldn't be too hard... hopefully.
I'm running Nginx with two Laravel projects being hosted. The following directories are where they are being stored.
/var/www/site.kara
/var/www/site.arkmanager
The location of the projects are http://10.0.0.2/kara and http://10.0.0.2/arkmanager.
The site.kara laravel project is loading images just fine. However I have an issue with the site.arkmanager locating images in my CSS file. It is also having issues loading FontAwesome webfonts files as well, due to the css code looking in the root of the server and not in the project directory. (CSS Code Below) I look at the console and there is an error...
So, according to this error its trying to get the image file from the server root directory? It isn't adding the /arkmanager portion of the image location... the image location is: http://10.0.0.2/arkmanager/images/app/hero-background.png. So I'm thinking that I screwed up my NGINX default file somehow, even though its not giving me any errors when running sudo nginx -t. A little bit of insight would be helpful in solving this issue.
My CSS class property, if its relevant to the problem, putting here just in case:
#hero {
width: 100%;
height: 100vh;
background: url("/images/app/hero-background.png") top center;
background-size: cover;
position: relative;
}
Here is my /etc/nginx/sites-available/default file contents. I am not running SSL due to the fact that this is a local server (second computer) inside my internal network and not available to the public.
# HTTP Server Block
server {
# Port that the web server will listen on.
listen 80 default_server;
# Root Folder
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;
# IP ADDRESS
server_name _;
# Root Location
location / {
# URLs to attempt, including pretty ones.
try_files $uri $uri/ /index.php$is_args$args;
}
# Karas Worlds Nested Location
location /kara {
alias /var/www/site.kara/public/;
try_files $uri $uri/ #kara;
# PHP FPM configuration.
location ~ \.php$ {
#Include Fast CGI Snippets
include snippets/fastcgi-php.conf;
# Define the PHP Script Filename
fastcgi_param SCRIPT_FILENAME $request_filename;
# With php-fpm (or other unix sockets)
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
}
}
location #kara {
rewrite /kara/(.*)$ /kara/index.php?/$1 last;
}
# End Karas World Nested Location
# Ark Manager Nested Location
location /arkmanager {
alias /var/www/site.arkmanager/public/;
try_files $uri $uri/ #arkmanager;
# PHP FPM configuration.
location ~ \.php$ {
#Include Fast CGI Snippets
include snippets/fastcgi-php.conf;
# Define the PHP Script Filename
fastcgi_param SCRIPT_FILENAME $request_filename;
# With php-fpm (or other unix sockets)
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
}
}
location #arkmanager {
rewrite /arkmanager/(.*)$ /arkmanager/index.php?/$1 last;
}
# Ark Manager Nested Location
# PHP FPM configuration.
location ~ \.php$ {
#Include Fast CGI Snippets
include snippets/fastcgi-php.conf;
# With php-fpm (or other unix sockets)
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
}
# We don't need .ht files with nginx.
location ~ /\.ht {
deny all;
}
}
Also side note here is a screenshot of my npm run dev command running and compiling successfully, just in case anyone thought that the CSS file might have been borked.
This is what what the application looks like on my local dev machine:
https://prnt.sc/vtu6f6
https://prnt.sc/vtu6y0
This is what it looks like on the local ubuntu server running nginx:
https://prnt.sc/vtu6r5
https://prnt.sc/vtu77h

Nginx config - Why Yii2 application is not served after I added "location = /"?

I have Nginx configured and running for some time now. It is handling 3 applications.
Most important is the application #1. It consists of 3 modules: account, participant and admin. Therefore these addresses allow logging into the application for different types of users:
domain.com/account/login, domain.com/participant/login or domain.com/admin/login
Also the homepage domain.com is handled by the same application.
Nginx config was very simple to serve this:
location / {
try_files $uri $uri/ /index.php?$args;
}
root $rootPath;
index index.html index.php;
# PHP
location ~ \.php$ {
fastcgi_pass unix:/var/run/php5-fpm.sock;
include fastcgi_params;
try_files $uri /index.php?$args;
}
Thanks to this Home page and Modules were handled by Yii2.
I don't think it should be relevant but there are 2 more apps configured in nginx under locations /application2 and / application3. One is php Zend app and the other - php and javascript app.
Now I need to replace homepage with Wordpress one-pager. Desired solution is to have:
homepage served by Wordpress at domain.com
Yii2 application modules working as before, so that all /participant, /account and /admin locations are served by index.php at $rootPath
2 other applications working as before at domain.com/application2 and domain.com/application3.
For handling Wordpress page I have added one section location = / to redirect main address to Wordpress app. But I cannot add nested location for PHP because I am getting this error: location "/(.+.php$)" cannot be inside the exact location "/"
So I changed the approach. I have set global root to Wordpress path: root $wpPath; and added locations for account, participant and admin modules:
location /account {
root $rootPath;
if (-f $request_filename) {
break;
}
# process PHP here
location ~ /account/(.+\.php$) {
alias $rootPath/$1;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
}
The main page (Wordpress) works perfectly fine. But the Yii2 application modules (like /aaccount) return 404 - Not found. What is wrong with above setup?

rewrite rules for nginx and Codeigniter

I have implemented a php application in codeigniter and now want to deploy it to the nginx server. Before deploying I checked my nignx configuration on my localhost using MAMP server. It is working correctly. But, this configuration is not working on the live server. As a beginner in nginx, I am not understanding where is the mistake here. In live server, I can not write in the main nginx.conf file. I have a separate configuration file like "abc" for my application "abc". And all my application files are under "abc/xyz" directory. Here is my sample confuguration,
location /abc {
root /srv/www/htdocs/apps/;
index index.html index.htm index.php;
location /xyz {
try_files $uri $uri/ /abc/xyz/index.php;
}
location ~ \.php(\/(\w+))*$ {
try_files $uri =404;
rewrite (.+)\.php(\/(\w+))*$ $1.php break;
include /etc/nginx/fastcgi_params;
fastcgi_index index.php;
fastcgi_pass unix:/var/run/php-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
Here, I can see my welcome page https://myapplication/abc/xyz. But if I want to navigate other pages like https://myapplication/abc/xyz/other_pages, it is showing "404 Page not found". I have checked the other solutions but none of them is not working in this case. Thanks in advance for the help!
The location /xyz block is nested within the location /abc block. The nested block is required to precess URIs with a prefix of /abc/xyz.
If there are other regular expression location blocks surrounding your location /abc block, you should use the^~` modifier.
For example:
location ^~ /abc {
...
location /abc/xyz {
...
}
...
}
See this document for more.
Sorry for the late answer. It was actually very silly mistake. My controller page name was in small character. This is why it was not working. My configuration is okay. The first letter of the controller page should be in capital character. For example, my controller name is Home. So my php file name must be Home.php not home.php.

Nginx location: 403 error / File not found

I set up my domain on my server using nginx. So far so good my homepage works. But now I wanna add some locations for later test of programming. My plan is to call diffrent projects like mydomain.com/php/myprogramm.php
So I add some folder in /var/www/mydomain.com/php (my side index is in /var/www/mydomain.com/html)
Entering www.mydomain.com/php/ leads to an 403 error and mydomain.com/php/myprogramm.php says File not found...
this is my nginx file:
server {
listen 80 default_server;
#listen [::]:80 default_server ipv6only=on;
# Make site accessible from http://localhost/
server_name mydomain.com www.mydomain.com;
location / {
root /var/www/mydomain.com/html;
index index.html index.htm;
}
location /php/ {
root /var/www/mydomain.com;
}
location /js/ {
root /var/www/mydomain.com;
}
location /node/ {
root /var/www/mydomain.com;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
# # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
#
# # With php5-cgi alone:
# fastcgi_pass 127.0.0.1:9000;
# # With php5-fpm:
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
}
Of course when I set up my domain I also set sudo chown -R www-data:www-data /var/www/mydomain.com/html and sudo chmod 755 /var/www
Some ideas someone? :/
Problems analysis
The first golden rule is:
nginx always serves request from a single location only. (Re-)read http://nginx.org/en/docs/http/request_processing.html.
Based on your configuration:
Requests to (www.)mydomain.com/php/<whatever> for files not ending with .php will be served by location /php/ from /var/www/mydomain.com/php/<whatever>
Requests to (www.)mydomain.com/<whatever>.php will be served by location ~\.php$ from <default root ('html' by default)>/<whatever>.php
The first problem here is that you are not serving .php files from where you think you are. Learn from location documentation how the location block serving a request is chosen.
You will note that the 'File not found' error was not an nginx error, but a message generated by PHP. That helps to know whether the problem comes from (frontend or backend).
Now about that 403: it seems nginx has trouble accessing the location where it is supposed to serve content from. Check /var/www/mydomain.com/php/ (directory + contents) rights.
Proposed pieces of advice
Your configuration looks suboptimal.
If you use the same root in lots of location blocks, why not moving it one level upper so it becomes the default (which yo ucan override in specific locations where needed)?
You can used nested locations, ie to solve you PHP files serving problem. Note that it is always a good idea to enclose regex locations inside prefix locations (What is the difference? Read location documentation). The reason is regex locations are order-sensitive, which is bad for maintenance. Prefix locations are not since only the longest match with a request URI will be chosen.
Here is a propsed updated version of part of your configuration:
root /var/www/mydomain.com;
location / {
root /var/www/mydomain.com/html;
index index.html index.htm;
}
location /php/ {
location ~ \.php$ {
# Useless without use of $fastcgi_script_name and $fastcgi_path_info
# Moreover, requests ending up here always end with .php...
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
# You seem to have copy-pasted this section without understanding it.
# Good understanding of what happens here is mandatory for security.
}
}
I suggest you read the documentation about fastcgi_split_path_info, $fastcgi_script_name and $fastcgi_path_info.
For my testing right now I fixed the issue quite simply.
I forogt to check my php.ini and change the cgi.fix_pathinfo to 0
Also I changed the group for my folders (still had root inside) to www-data.
At the end I updated my configuration: I set root /var/www/mydomain.com; in my server block (server{})
That's all I did.
But I will keep your advice in mind for later issues.
Thanks for your help I appreciate it.

Resources