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

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.

Related

Nginx rewrite rule inside location with php enabled

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

Configuring php with nginx inside a reactjs app

I have a nginx configuration for a react app. I however would also like to include a sitemap.php that I build dynamically with php.
So here is my nginx config:
server {
listen 80;
listen [::]:80;
server_name mysite.com;
index index.html index.htm;
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
}
location /sitemap.xml {
alias /var/www/web-app/public/sitemap.php;
}
location / {
root /var/www/web-app/public;
try_files $uri $uri/ /index.html;
default_type "text/html";
}
}
The snippets file consist of this:
# regex to split $uri to $fastcgi_script_name and $fastcgi_path
fastcgi_split_path_info ^(.+\.php)(/.+)$;
# Check that the PHP script exists before passing it
try_files $fastcgi_script_name =404;
# Bypass the fact that try_files resets $fastcgi_path_info
# see: http://trac.nginx.org/nginx/ticket/321
set $path_info $fastcgi_path_info;
fastcgi_param PATH_INFO $path_info;
fastcgi_index index.php;
include fastcgi.conf;
Also, this is hosted on an Ubuntu 16.04 digitalocean VPS.
My react app still loads fine. It is based on the index.html in my site root (/var/www/web-app/public). If I put test.php in the public folder, I get a 404 error. For my sitemap.xml alias, it forwards correctly to sitemap.php (also in public) but the php does not render.
So my two biggest issues here:
1. Why am I getting a 404 on /mysite.com/test.php?
2. And why is my php not rendering when it does work? (i.e. sitemap.php)
You are missing a root statement for your location ~ \.php$ block, so your PHP files will not be found. As this seems to be a common root with the location / block, simply move the statement up to server block scope:
root /var/www/web-app/public;
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
}
location / {
try_files $uri $uri/ /index.html;
default_type "text/html";
}
There are a number of ways to redirect /sitemap.xml to /sitemap.php, but a rewrite...last will be simplest and invisible to users:
location = /sitemap.xml {
rewrite ^ /sitemap.php last;
}
See this document for location syntax, and this one for the rewrite directive.

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?

Nginx with CakePHP in subfolder not serving static files (CSS, JS, IMG)

I am trying to setup a Nginx server configuration to serve a CakePHP installation from and to a subfolder.
URL: https://sub.domain.com/cakefolder
Folder on system: /var/www/domain/sub/cakefolder
So i am using a sub folder for both the URL as well as on the system. Now it took me a while to figure the following config out with which requests are properly handled by CakePHP. This means it's correctly bootstraping and handling controllers.
What doesn't work however, is serving static files from the webroot directory (e.g. *.css files) as those are all interpreted as CakePHP controllers leading to a CssController could not be found. error.
My site.conf:
server {
listen *:80;
listen *:443 ssl;
server_name sub.domain.com;
ssl_certificate ./ssl/domain.crt;
ssl_certificate_key ./ssl/domain.key;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
if ($ssl_protocol = "") {
rewrite ^ https://$server_name$request_uri? permanent;
}
root /var/www/domain/sub/cakefolder/;
autoindex off;
index index.php;
location /cakefolder {
root /var/www/domain/sub/cakefolder/app/webroot/;
try_files $uri $uri/ /cakefolder/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;
}
How do I stop Nginx from routing existing static files through the FastCGI PHP interpreter?
Based on https://stackoverflow.com/a/22550332/671047 I already tried replacing my location /cakefolder { ... } with
location ~ /cakefolder/(.*) {
try_files /cakefolder/$1 /cakefolder/$1/ /cakefolder/index.php?$args;
}
but this leads to a redirection loop causing a HTTP 500 error.
Solution (thanks Pete!):
I found the following additional directive to be working for this specific setup. This might not be the most elegant solution but who cares, glad it's working for now.
location ~* /cakefolder/(.*)\.(css|js|ico|gif|png|jpg|jpeg)$ {
root /var/www/domain/sub/cakefolder/app/webroot/;
try_files /$1.$2 =404;
}
you could catch it early:
location ~* \.(css|js|ico)$ {
try_files $uri =404;
}
i have a similar setup and that worked for me when i experienced the same thing (just not cake.) I won't lie, i never understood why the try_files w/redirect always failed on existing static files, where as throwing a try_files like ^above finds the file np. ideas on that? (perhaps today is a source-reading day)

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