Cakephp 3 NGINX URL rewriting issue - cakephp

I am trying to setup CakePHP 3 on Linux server CentOS 7 OS. I have created a project using composer and everything installs correctly with proper permissions. In my virtual host file I have configured config file according to official document
server {
listen 80;
server_name remote.inodd.com;
## redirect http to https ##
rewrite 301 https://$server_name$request_uri permanent;
#rewrite 301 https://$server_name$request_uri permanent;
}
server {
# listen 80;
listen 443 ssl;
server_name remote.inodd.com;
ssl_certificate /home/vhost/www/domain/ssl/self-ssl.crt;
ssl_certificate_key /home/vhost/www/domain/ssl/self-ssl.key;
# note that these lines are originally from the "location /" block
root /home/vhost/www/domain/public_html;
index index.php;
access_log /home/vhost/www/domain/logs/access_log;
error_log /home/vhost/www/domain/logs/error_log;
location / {
#try_files $uri $uri/ =404;
try_files $uri $uri/ /index.php?url=$request_uri;
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
so when I browse the url https://remote.inodd.com/development/, I get default landing page showing status of cakephp configuration. Everything checks out normal even database connectivity. But I get below warning that rewriting is not configured correctly.
URL rewriting is not properly configured on your server.
1) Help me configure it
2) I don't / can't use URL rewriting
Not sure what else I am missing here as I am able to setup non-cakephp sites and they are working fine.

Got it working. Thanks to a great solution provided by Jamie.
Here is my final working code:
server {
listen 80;
server_name remote.inodd.com;
## redirect http to https ##
rewrite ^ https://$server_name$request_uri? permanent;
}
server {
# listen 80;
listen 443 ssl;
server_name remote.inodd.com;
ssl_certificate /home/vhost/www/domain/ssl/self-ssl.crt;
ssl_certificate_key /home/vhost/www/domain/ssl/self-ssl.key;
access_log /home/vhost/www/domain/logs/access_log;
error_log /home/vhost/www/domain/logs/error_log;
location / {
root /home/vhost/www/domain/public_html/development/webroot;
index index.php index.html index.htm;
if (-f $request_filename) {
break;
}
if (-d $request_filename) {
break;
}
rewrite ^(.+)$ /index.php?q=$1 last;
}
location ~ .*\.php[345]?$ {
include fastcgi_params;
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME
/home/vhost/www/domain/public_html/development/webroot$fastcgi_script_name;
}
}

If your domain has a conf file in vhost then please add this line to your vhost domain.conf file.
location / {
try_files $uri $uri/ /index.php?$uri&$args;
rewrite ^/$ /app/webroot/ break;
rewrite ^(.*)$ /app/webroot/$1 break;
}
location /app/ {
rewrite ^/$ /webroot/ break;
rewrite ^(.*)$ /webroot/$1 break;
}
location /app/webroot/ {
if (!-e $request_filename){
rewrite ^(.*)$ /index.php break;
}
}
The main problem is cakephp has 3 different htaccess,
first is the main dir of project
second one is in app folder
third is webroot folder.
So we have to handle all of this. I found a solution like that.

Related

Setup two React websites on the same domain with NGINX, deployed to separate folders

I am trying to configure NGINX to serve two separate websites on the same domain, with one of the websites being served at the top level of the domain, and the other accessible from domain.com/gol
The first websites files are on the server located at /var/www/morderindustries.com/public
The second websites files are on the server located at /var/www/morderindustries.com/gol
This is my nginx config for the website:
server {
listen 443 ssl default_server;
listen [::]:443 ssl default_server;
ssl on;
ssl_certificate /etc/ssl/ssl-bundle.crt;
ssl_certificate_key /etc/nginx/ssl-certs/www.morrder.com.key;
server_name morrder.com www.morrder.com;
# index index.html
error_log /var/log/nginx/morderindustries.com.error.log;
access_log /var/log/nginx/morderindustries.com.access.log;
location / {
root /var/www/morderindustries.com/public;
try_files $uri $uri/ /index.html =404;
}
location /gol {
root /var/www/morderindustries.com/gol;
try_files $uri $uri/ /index.html =404;
}
error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /var/www/morderindustries.com/public;
}
}
With this setup, https://morrder.com works fine and serves the website that is located in the /public folder.
I expect https://morrder.com/gol to serve the second website in the /gol folder, but the result is the following:
Does anyone have a clue as to what I've screwed up?

Nginx configuration issue with https on windows

I am trying to deploy my react app to nginx server. Http configuration is working fine, but https is not working. Here is my configuration file.
events {
worker_connections 1024;
}
http {
server {
listen 80;
server_name example.com;
root D:\\path-to-build\\build;
index index.html;
location / {
try_files $uri /index.html =404;
}
}
server {
listen 443 ssl;
server_name example.com;
keepalive_timeout 70;
ssl_certificate C:\\Certbot\\live\\domain\\fullchain.pem;
ssl_certificate_key C:\\Certbot\\live\\domain\\privkey.pem;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers HIGH:!aNULL:!MD5;
#...
access_log D:\\path-to-log\\access.log;
error_log D:\\path-to-log\\error.log;
root D:\\path-to-build\\build;
index index.html;
location / {
try_files $uri /index.html =404;
}
}
}
Your config file looks good. But in your SSL server config i do not see any reference to root or index directives.
Add this to your SSL server as well:
root D:\\path-to-build\\build;
index index.html;
Also as the error implice ("The connection has timed out") can you please try changing the value to keepalive_timeout 70s; Note that the default value is 75 seconds.
Configuration is correct only, but in my server machine 443 post is not open for outside. That's the reason I am getting connection timed out.

nginx redirect all urls to index.html

I am trying to configure nginx server according to following guide: https://gkedge.gitbooks.io/react-router-in-the-real/content/nginx.html
But looks like it doesn't work, I am trying to use with my docker image of nginx. So,
Dockerfile:
FROM nginx:1.11.5
COPY /dist /usr/share/nginx/html
COPY /vhosts.conf /etc/nginx/conf.d/vhosts.conf
vhosts.conf:
server {
listen 80 default_server;
server_name /usr/share/nginx/html;
root /usr/share/nginx/html;
index index.html index.htm;
location ~* \.(?:manifest|appcache|html?|xml|json)$ {
expires -1;
# access_log logs/static.log; # I don't usually include a static log
}
location ~* \.(?:css|js|json)$ {
try_files $uri =404;
expires 1y;
access_log off;
add_header Cache-Control "public";
}
# Any route containing a file extension (e.g. /devicesfile.js)
location ~ ^.+\..+$ {
try_files $uri =404;
}
# Any route that doesn't have a file extension (e.g. /devices)
location / {
try_files $uri $uri/ /index.html;
}
}
Trying to open a page that doesn't exists.
GET localhost/somestupidrandomurl
nginx error log:
[error] 7#7: *3 open() "/usr/share/nginx/html/somestupidrandomurl" failed (2: No such file or directory), client: 172.17.0.1, server: localhost, request: "GET /somestupidrandomurl HTTP/1.1", host: "localhost"
Thanks in advance.
After a deep search, I realize that the thing is, there is one more default.conf file at /etc/nginx/conf.d/ path and that config overwrites my config.
I simply replace the Dockerfile line 3 as
COPY /vhosts.conf /etc/nginx/conf.d/default.conf and works like a magic.

Nginx Rewrite 414-URI Too Large

I am trying to setup CakePHP on an nginx server. Following their blog tutorial on setting up URL rewriting for nginx, I ended up with this in my sites-available/default file:
server {
listen 80;
server_name localhost;
rewrite ^(.*) localhost$1 permanent;
}
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
server_name localhost;
root /usr/share/nginx/html/StudentRecords/webroot;
index index.php index.html index.htm;
error_page 404 /404.html;
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;
}
}
/usr/share/nginx/html/StudentRecords/ is the top directory for my CakePHP app.
When I go to the URL localhost, it shows me the default "Welcom to nginx" page. If I try to go to localhost/StudentRecords/webroot, I get a 414 error, and end up with a URL like this:
http://localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/localhost/StudentRecords/webroot/
I'm having trouble understanding why the server root doesn't point to StudentRecords/webroot, and why my url is multiplying like that.
just remove the first server block
It looks like the rewrite ^(.*) localhost$1 permanent; line (and first code block) was unnecessary. Removing the first block, as well as clearing my browser cache has resolved both issues.

Angular - Nginx rewrites domain/foo to angular root but not domain/foo/bar

Trying to move from Apache to Nginx using Angular JS with a Laravel backend.
The front and back ends are completely independent of each other, as in var/www/angular and var/www/laravel. They are served from the same domain - mysite.com (angular) and laravel API routes got through mysite.com/api
I've got everything working including html5mode and browser refresh for front-end routes like mysite.com/foo
The problem is, refreshing or manually entering mysite.com/foo/bar does not work. Instead the html is served without css, meaning that it is not being routed to angular.
Any advice is greatly appreciated.
Here is my current Nginx vhost config:
server {
listen 80 default_server;
server_name default;
root /home/forge/default/laravel/public;
# FORGE SSL (DO NOT REMOVE!)
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
# ssl_certificate;
# ssl_certificate_key;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
index index.html index.htm index.php;
charset utf-8;
location / {
root /home/forge/default/angular/dist;
try_files $uri $uri/ /index.html;
}
location /lvl/ {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \/lvl\/index\.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi_params;
include fastcgi.conf;
#This specifically sets so the /api/ portion
#of the URL is not included
fastcgi_param SCRIPT_FILENAME $document_root/index.php;
fastcgi_param PATH_INFO $fastcgi_path_info;
#fastcgi_param ENV production;
}
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
access_log off;
error_log /var/log/nginx/default-error.log error;
error_page 404 /index.html;
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
}
EDIT 1:
Per raam86's comment i tried a version of the rewrite he reffered me to with the same exact results, problem still persists. Here are the edits i made:
location / {
root /home/forge/default/angular/dist;
try_files = $uri #missing;
}
location #missing {
rewrite ^/$ /index.html last;
}
Turns out the root of the problem was that grunt wasn't applying the base / before the style declarations, even though i had
<base href="/">
set in the head of my index.html
The solution was a simple one... simply move the base tag to the top of the head section instead of the bottom!

Resources