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?
Related
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.
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.
I'm trying to get wordpress running in a subdirectory of an existing nginx (v1.14) server. The rest of the server works fine. I've copied the relevant lines of my nginx.conf below. If I remove the lines that refer directly to the wordpress (wp) directory, then I get an error complaining that wp has called an undefined function "mysql_connect". When I leave the lines that directly refer to the wp directory, it complains about permission denied.
I've looked through a bunch of other posts about this but I can't seem to find any solutions that solve the issue for me. I've tried:
adding define( 'WP_USE_EXT_MYSQL', true ); to wp-config.php and toggling the boolean
adding / removing $uri/ from nginx.conf
chmodding the wordpress dir and contents (755 for the dir and 644 for the contents)
adding / removing index.php to the wordpress location definition in nginx.conf
I have no idea how to troubleshoot this any further.
...
http {
...
upstream php {
server unix:/tmp/php-cgi.socket;
server 127.0.0.9000;
}
...
server_name domain.tld
root /some/dir
location / {
index index.html index.htm index.php;
}
...
location /wordpress {
try_files $uri $uri/ /wordpress/index.php?$args;
}
location ~ \.php$ {
fastcgi_pass unix:/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
include fastcgi.conf;
fastcgi_intercept_errors on;
fastcgi_split_path_info ^(/wordpress)(/.*)$;
}
...
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
Basically I have a site, called example.com. Within this site, there is a WordPress-Blog, wich should be accessible under "example.com/blog/". The actual directory where the blog is installed is not "/blog/", but "/blog/de/", as this project has multiple stand-alone-blogs for different languages.
The problem is, that WordPress cannot access to wp-content and wp-include files, because WordPress tries to load css (and other) files via example.com/blog/wp-content/....
So i want to get my Blog running under "example.com/blog", but all WordPress-Files should be loaded from /blog/de/.
This is my nginx-vhost-config for /blog-Location:
location /blog {
index index.php;
try_files $uri $uri/ /de/index.php?$args;
rewrite ^(/wp-content/.*)$ /de/wp-content/$1 last;
rewrite ^/blog/(.*)+$ /blog/de/index.php?$1;
}
rewrite /wp-admin$ $scheme://$host$uri/ permanent;
I am totally new to nginx, so every possible solution is welcome!
Essentially you need to alias the location /blog to the path /blog/de. The complication is the way in which PHP files are handled, particularly when other applications are hosted within the same domain.
First internally rewrite /blog to /blog/de:
location /blog {
rewrite ^/blog(.*)$ /blog/de$1 last;
}
Then implement the /blog/de location with a nested location to handle PHP:
set $wordpress /blog/de/index.php;
location ^~ /blog/de {
# root inherited from server container???
index index.php;
try_files $uri $uri/ $wordpress;
location ~ \.php$ {
try_files $uri $wordpress;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass ...;
}
}
I make /blog/de a priority prefix location, to avoid a conflict with any other php location. But that would not be necessary if this is the only application within this server block.