Nginx rewrite rule inside location with php enabled - php

I need to have a php application inside my default server configuration.
Also this application it was previously allocated on a apache server with rewrite rules on the .htaccess file that I translated to nginx.
I'm trying to configure it all like that:
server {
listen [::]:443;
server_name _;
root /var/www/html;
index index.php index.html index.htm index.nginx-debian.html;
location /admin {
alias /var/www/sefoanco/html;
index index.php index.html index.htm;
try_files $uri $uri/ #admin;
rewrite ^(/admin/.*)/login/ /login/controller.php break;
rewrite ^(/admin/.*)/observacions/ /observacions/controller.php break;
rewrite ^(/admin/.*)/usuari/ /usuari/controller.php break;
rewrite ^(/admin/.*)/llistat/ /llistat/controller.php break;
location ~ /admin/.+\.php$ {
include snippets/fastcgi-php.conf;
fastcgi_param SCRIPT_FILENAME $request_filename;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
include fastcgi_params;
}
}
location #admin {
rewrite /admin/(.*)$ /admin/$1 last;
}
}
If I access to localhost/admin/login I get a 403 forbidden error.
If I add the controller.php to the index files, it answer correctly with the php response, so I think that the php is well configured.
So I think that I forget something to config.

Finally the problem was that the application are not made to work inside a path. If I see the application configuration I saw the following:
$_DOCUMENT_ROOT = $_SERVER["DOCUMENT_ROOT"];
So, is easy to make it working on paths but the application doesn't work with relative paths so after make a call inside login, will search the auth method inside the root path.
So is needed to rewrite some parts of the application before it can work inside a path.
Anyway, I get the login page working just deleting rewrite rules and adding controller.php on the index param. Something like should work:
server {
listen [::]:443 ;
server_name _;
root /var/www/html;
index index.php index.html index.htm index.nginx-debian.html controller.php;
location /admin {
try_files $uri $uri/ =404;
}
location ~ \.php($|/) {
include snippets/fastcgi-php.conf;
fastcgi_param SCRIPT_FILENAME $request_filename;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
include fastcgi_params;
}
}

Related

How to route request url subfolder paths to specific php pages using nginx and php-fpm

I'm using a local nginx server for the first time to set up a website i'm building and i'm having trouble setting up the nginx config to handle url requests the way I want. My website serves multiple php pages as the user navigates through the website. When developing the site initially using a local php server, I used GET requests with window.location.href changes for site navigation. For example:
http://localhost:8000/shop.php?filter=all&sort=id_asc&page=3
However, since its going to be an ecommerce website for a small business, I wanted to handle the URLs in a cleaner and more professional manner.
My site structure looks something like this:
Website:
->index.php
->shop.php
->about.php
->product-page.php
->/css/
->/javascript/
->/php/
I want to configure nginx to route url paths in the following way
www.mywebsite.com -> routes to index.php
www.mywebsite.com/shop -> routes to shop.php
www.mywebsite.com/shop/anything -> routes to shop.php
www.mywebsite.com/about -> routes to about.php
www.mywebsite.com/product -> routes to product-page.php
www.mywebsite.com/product/anything -> routes to product-page.php
I've tried numerous suggestions over a couple of days before asking here but everything failed due to one reason or another, 404s, 500 internal errors, and redirect loops. I'm hoping to gain some inside here while I move onto other aspects of the site, so as to stop beating my head against the wall. Here is the state of my nginx conf at this moment:
server {
listen 80 ;
listen [::]:80 ;
server_name localhost;
root /var/www/html/reagansrockshop;
index index.php index.html;
location / {
try_files $uri $uri/ =404;
}
location = /shop {
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
fastcgi_index shop.php;
try_files $uri /shop.php;
}
location /shop/ {
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
try_files $uri /shop.php;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
How could I go about solving this? And if there is a better standard in structuring a website and its URLS please let me know. This is my first website and first time using nginx - so i'm a little naive on best practices.
If you need a certain php script to be responsible for a whole path, you need a config like this:
root /var/www/html/reagansrockshop; # root directive is necessary to define where '/' is
location /shop/ { # this means "all URLs starting with '/shop/' "
index /shop.php; # be careful with path to the file here
}
Although I would rather recommend a more traditional and cleaner project structure.
In your project root create two directories: shop and product. Move shop.php and product-page.php into designated folder and rename both to index.php. Your nginx config for this structure will be like this:
server {
listen 80 ;
listen [::]:80 ;
server_name localhost;
root /var/www/html/reagansrockshop;
index index.php index.html;
location / {
index index.php;
try_files $uri $uri/ =404;
}
location /shop/ {
try_files $uri $uri/ /shop/index.php?$args;
}
location /product/ {
try_files $uri $uri/ /product/index.php?$args;
}
location ~ \.php$ {
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}

Nginx/Fcgi: index.php issue with pseudo-alias location

Nginx 1.6.2 on Debian Jessie
I want to map all example.com/forum/ requests to /path/to/htdocs/phpbb and cut off the /forum/ part in the URI. Someone on Stackoverflow recommended the "rewrite" solution instead of "alias", because there are some bugs.
server
{
listen [::]:80;
server_name example.com;
root /var/www/html;
index index.php index.html;
#try_files $uri $uri/ =404;
location /forum/
{
root /path/to/htdocs/phpbb;
rewrite ^/forum/(.*)$ /$1 break;
location ~ .+\.php$
{
rewrite ^/forum/(.*)$ /$1 break;
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php5-fpm.sock;
}
}
}
The example configuration works fine – example.com/forum/viewtopic.php executes the script /path/to/htdocs/phpbb/viewtopic.php – but example.com/ (index.php) doesn't work:
"/var/www/html/index.php" failed (2: No such file or directory)
After removing the "index" line from server block:
directory index of "/path/to/htdocs/phpbb/" is forbidden
After moving the "index" and/or "try_files" line(s) into the location block:
index.php served without passing over to php-fpm…
Ok, what's wrong with my config? Any hints?
Ok, alias is buggy (rewrite too…), but if you avoid try_files and use if instead (even if evil…) it should work!
server
{
listen [::]:80;
server_name example.com;
root /var/www/html;
location /forum/
{
alias /path/to/htdocs/phpbb/;
index index.php index.html;
location ~ "^(/forum/)(.+\.php)(/.+){0,1}$"
{
if (!-f $document_root$2)
{
return 404;
}
fastcgi_index index.php;
include fastcgi.conf;
fastcgi_param SCRIPT_FILENAME $document_root$2;
fastcgi_param SCRIPT_NAME $1$2;
fastcgi_param PATH_INFO $3;
fastcgi_pass unix:/var/run/php5-fpm.sock;
}
}
}
phpinfo() looks fine, but one question remains: Is it secure?

AngularJS and Laravel config for nginx. Running php from url segment

I have problem configuring laravel configuration that would run from /api/ segment of main domain.
Idea is to serve at subdomain.domain.com main Angular aplication and in subdomain.domain.com/api/ laravel php server configuration.
UPDATE #1
I have managed to run laravel from /api/ segment with this configuration
server {
listen 80;
server_name subdomain.domain.com;
root /var/www/subdomain.domain.com/client/monkey/dist;
access_log /var/www/subdomain.domain.com.access.log;
error_log /var/www/subdomain.domain.com.error.log;
rewrite_log on;
index index.php index.html;
location / {
root /var/www/subdomain.domain.com/client/monkey/dist;
index index.html;
if (!-e $request_filename){ # handles page reload
rewrite ^(.*)$ /index.html break;
}
}
location /api/ {
root /var/www/subdomain.domain.com/server/;
try_files $uri $uri/ /api/index.php$is_args$args;
}
location ~ /api/.+\.php$ {
root /var/www/subdomain.domain.com/server/;
rewrite ^/api/(.*)$ /$1 break;
fastcgi_pass unix:/var/run/php5-fpm.sock;
include /etc/nginx/fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_index index.php;
fastcgi_param MONKEY_ENV production;
fastcgi_split_path_info ^(.+?\.php)(/.*)?$;
# fastcgi_split_path_info ^(.+\.php)(.*)$;
}
}
Only problem that has left is that when i'm using subdomain.domain.com/api/segment1/segment... then I need to add in routes.php
Route::group(['prefix' => 'api'], function () {
// my routes settings
});
As laravel is starting from suddomain.domain.com/ as root path. How can i rewrite /api/ so that laravel takes /api/ as routing starting point ?
If I hava understanded your problem, I guess that you could solve this with the alias directive instead root. Using the root directive Nginx just takes de informed location and concat it at the end of the informed root path.
So, in your case, this conf:
location /api/ {
root /var/www/subdomain.domain.com/server/;
try_files $uri $uri/ /api/index.php$is_args$args;
}
nginx would resolved it to /var/www/subdomain.domain.com/server/api as the final path.
Using the alias directive, this conf,
location /api {
alias /var/www/subdomain.domain.com/server/;
try_files $uri $uri/ /api/index.php$is_args$args;
}
would be resolved to anything that's aim to '/api' but nginx is not concatening the 'api' string in the final path it uses.
http://nginx.org/en/docs/http/ngx_http_core_module.html#alias
Hopes this helps.

Serving php files from different locations in nginx

I have my main website and wordpress in different directories on my server on which I use nginx as the web server. The main website is in /home/me/www and Wordpress is in /home/me/wordpress. I need to have them in separate directories this way for a particular reason. How do I specify this in the nginx configuration file? I currently have the following and it does not work:
location / {
root /home/me/www;
index index.php index.html index.htm;
}
location /blog {
root /home/me/wordpress;
index index.php index.html index.htm;
}
location ~ \.php$ {
set $php_root /home/me/www;
if ($request_uri ~ /blog) {
set $php_root /home/me/wordpress;
}
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;
}
It currently returns HTTP 404 when I try to access http://mydomain/blog
Check out this question and the Nginx Manual.
Try changing your blog line to:
location ^~ /blog/ {
root /home/me/wordpress;
index index.php index.html index.htm;
}
I struggled with this for hours now and finally reached the working configurations as the following:
location /php-app {
passenger_enabled off;
alias /path/to/php-app/$1;
index index.php index.html;
try_files $uri $uri/ #rewrite;
}
location #rewrite {
rewrite ^/php-app(.*)$ /index.php?q=$1 last;
}
location ~ \.php$ {
alias /path/to/php-app/$1;
rewrite ^/php-app(.*)$ $1 last;
passenger_enabled off;
fastcgi_pass unix:/tmp/php-fpm.socket;
fastcgi_index index.php;
include /etc/nginx/fastcgi_params;
fastcgi_param SCRIPT_FILENAME /path/to/php-app$fastcgi_script_name;
fastcgi_intercept_errors on;
}
Just adding more detail in #Nick Presta answer.
^~: If a carat and tilde modifier is present, and if this block is selected as the best non-regular expression match, regular expression matching will not take place.
Checkout These differences enter link description here

How do I configure nginx rewrite rules to get CakePHP working on CentOS?

Hi somebody please help me out, I’m trying to setup a cakephp environment on a Centos server running Nginx with Fact CGI. I already have a wordpress site running on the server and a phpmyadmin site so I have PHP configured correctly.
My problem is that I cannot get the rewrite rules setup correct in my vhost so that cake renders pages correctly i.e. with styling and so on. I’ve googled as much as possible and the main consensus from the sites like the one listed below is that I need to have the following rewrite rule in place
location / {
root /var/www/sites/somedomain.com/current;
index index.php index.html;
# If the file exists as a static file serve it
# directly without running all
# the other rewrite tests on it
if (-f $request_filename) {
break;
}
if (!-f $request_filename) {
rewrite ^/(.+)$ /index.php?url=$1 last;
break;
}
}
http://blog.getintheloop.eu/2008/4/17/nginx-engine-x-rewrite-rules-for-cakephp
problem is these rewrite assume you run cake directly out of the webroot which is not what I want to do. I have a standard setup for each site i.e. one folder per site containing the following folders log, backup, private and public. Public being where nginx is looking for its files to serve but I have cake installed in private with a symlink in public linking back to /private/cake/
this is my vhost
server {
listen 80;
server_name app.domain.com;
access_log /home/public_html/app.domain.com/log/access.log;
error_log /home/public_html/app.domain.com/log/error.log;
#configure Cake app to run in a sub-directory
#Cake install is not in root, but elsewhere and configured
#in APP/webroot/index.php**
location /home/public_html/app.domain.com/private/cake {
index index.php;
if (!-e $request_filename) {
rewrite ^/(.+)$ /home/public_html/app.domain.com/private/cake/$1 last;
break;
}
}
location /home/public_html/app.domain.com/private/cake/ {
index index.php;
if (!-e $request_filename) {
rewrite ^/(.+)$ /home/public_html/app.domain.com/public/index.php?url=$1 last;
break;
}
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /home/public_html/app.domain.com/private/cake$fastcgi_script_name;
include /etc/nginx/fastcgi_params;
}
}
Now like I said I can see the main index.php of cake and have connected it to my DB but this page is without styling so before I proceed any further I would like to configure it correctly. What am I doing wrong?
Thanks seanl
At a glance, your problem might be that you are not pointing nginx to the webroot of your app. Deploying to the root cake folder is really not the way to go under any web-server.
The following is a complete server-block I use running Cake apps. In reality I only have the first four lines and then include the rest from a separate file "cakephp.inc".
A note on the line "fastcgi_param SERVER_NAME $host;". This is because some of my apps use $_SERVER['SERVER_NAME'] and it does not have the same meaning in nginx as in Apache. If youe server has several server_name(s) defined nginx will always pass the first one to php.
server {
server_name cakeapp.example.com;
root /var/www/vhosts/cake/app/webroot;
access_log /var/log/nginx/cakeapp.access.log;
error_log /var/log/nginx/cakeapp.error.log;
listen 80;
rewrite_log on;
# rewrite rules for cakephp
location / {
index index.php index.html;
# If the file exists as a static file serve it
# directly without running all
# the other rewite tests on it
if (-f $request_filename) {
break;
}
if (!-f $request_filename) {
rewrite ^/(.+)$ /index.php?url=$1 last;
break;
}
}
location ~* \favicon.ico$ {
expires 6m;
}
location ~ ^/img/ {
expires 7d;
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include /etc/nginx/fastcgi_params;
fastcgi_param SERVER_NAME $host;
}
location ~ /\.ht {
deny all;
}
}
There's now official documentation on this issue, which I used and confirmed works.
The documentation states:
server {
listen 80;
server_name www.example.com;
rewrite ^(.*) http://example.com$1 permanent;
}
server {
listen 80;
server_name example.com;
# root directive should be global
root /var/www/example.com/public/app/webroot/;
index index.php;
access_log /var/www/example.com/log/access.log;
error_log /var/www/example.com/log/error.log;
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
try_files $uri =404;
include /etc/nginx/fastcgi_params;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
I got this working:
root DIR/app/webroot/;
location / {
index index.php index.html;
rewrite ^/$ /index.php?url=/;
if (!-e $request_filename) {
rewrite ^(/.*)$ /index.php?url=$1 last;
}
}
and then of course handlers for php and stuff...
It is not advisable to use 'IF' blocks inside a 'location' block.
Here is a more natural way to achieve the same, using regex locations.
In this example, CakePHP 2.x is the root app on a vhost (skipping common stuff like server_name , logs etc):
root /path/to/cakephp-2.x_root/app/webroot;
index index.php;
location ~ .+\.php$ {
try_files $uri =404; #handle requests for missing .php files
include fastcgi_params;
fastcgi_pass 127.0.0.1:7001; #the FPM pool port
}
location ~ ^/(.*) {
try_files $uri $uri/ /index.php?url=$1&$args;
}
Note that the .php location block is BEFORE the / location block. That's important because with regex locations, they are searched until the first match.
If you need to make it run in a sublocation, eg http://www.example.com/something/, here is how I managed to do it. First I had to do a symlink to trick nginx: extract cakephp-2.x somewhere, then in 'app/webroot' create a symlink to itself with the same name as the sublocation, e.g. 'ln -s ../webroot something' .
Then the following config works to access cackephp under /something/:
location ~ ^/something/.+\.php$ {
try_files $uri =404; #handle requests for missing .php files
root /path/to/cakephp-2.x_root/app/webroot;
include fastcgi_params;
fastcgi_pass 127.0.0.1:7001; #the FPM pool port
}
location ~ ^/something(?:/)(.*) {
root /path/to/cakephp-2.x_root/app/webroot;
index index.php;
try_files $uri $uri/ /something/index.php?url=$1&$args;
}
Symlinking can probably be avoided by using 'alias' istead of 'root' but I could not figure out how.
I had a bunch of issues setting up a CakePHP site that was running an older version of CakePHP 1.2 - going by the date of this post it might be around the time. I recently blogged about it and simply suggest upgrading or installing a fresh version of the Cake library and all problems went away.
Please use below code in
vi /etc/nginx/sites-available/domainname.com
server {
server_name cakeapp.example.com;
root /var/www/vhosts/cake/app/webroot;
access_log /var/log/nginx/cakeapp.access.log;
error_log /var/log/nginx/cakeapp.error.log;
listen 80;
rewrite_log on;
# rewrite rules for cakephp
location / {
index index.php index.html;
# If the file exists as a static file serve it
# directly without running all
# the other rewite tests on it
if (-f $request_filename) {
break;
}
if (!-f $request_filename) {
rewrite ^/(.+)$ /index.php?url=$1 last;
break;
}
}
location ~* \favicon.ico$ {
expires 6m;
}
location ~ ^/img/ {
expires 7d;
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include /etc/nginx/fastcgi_params;
fastcgi_param SERVER_NAME $host;
}
location ~ /\.ht {
deny all;
}
}
Its working for me.

Categories