Nginx with cakephp - configuration problem - cakephp

I am karthik. This is my first post here.
I have a vps running on centos with nginx server. I want a photogallery on my site. so i created a subdomain clicks.example.com. I am using a flash template called whitespace cms which uses cakephp. The problem is with the rewrite rules. I am not able to figure out the root directory.
This is my configuration file.
server {
listen 80;
server_name clicks.example.com;
access_log /home/www/example.com/logs/access.log;
error_log /home/www/example.com/logs/error.log;
rewrite_log on;
# rewrite rules for cakephp
location / {
root /home/www/example.com/clicks/fotoblog/app/webroot/;
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 ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /home/www/example.com/clicks/fotoblog/app/webroot$fastcgi_script_name;
include fastcgi_params;
}
}
I think i am messing up with subdomain and root of cakephp. any help will be appreciated. I tried for 8 hours :) still no luck. When i use the above configuration i get blank page and sometimes i get a 502 bad gateway error.
Update: okay, 502 errors was due to nginx php-fpm problems. now when i access clicks.example.com i am getting redirected to the blackened of the cms. clicks.example.com/users/login.. [actually the backened should be clicks.example.com/fotoblog/users/login, do know where i am going wrong]

You're doing nothing wrong! I had a similar issue as well and was able to fix it after hours of researching and wasting time - I simply did a fresh install of a new Cake version and it solved my nginx rewrite rule issues.

Related

Directus deployment on Elastic Beanstalk

I cloned Directus 8 from github. I run it in my local server. It worked fine without any problems.
Then I uploaded code to AWS Elastic Beanstalk (PHP, apache). but it showed 500 Internal Server Error.
error log: /var/www/html/directus/public/.htaccess: <IfModule not allowed here
I added .ebextensions/setup.config file to my root folder, like this.
files:
"/etc/httpd/conf.d/enable_mod_rewrite.conf":
mode: "644"
owner: root
group: root
content: |
AllowOverride All
but my Beanstalk said Unsuccessful command execution on instance id(s) 'i-0f6...'. Aborting the operation. and went to degrading state.
How to fix this?
This answer is for Directus 8 (PHP)
Tried almost all ways of apache settings using .ebextensions and .platform nothing worked.
Then tried NGINX with custom .platform configs. It worked. Answering the steps which I did, may be helpful to someone else, who has the same problem
Directus docs has some configs for NGINEX, go through it
create nginex.conf file under .platform/nginx folder
we are going to replace existing nginex.conf inside the beanstalk. copy existing nginex.conf using ssh to ec2 instance and add the custom configs mentioned in the docs and paste it to our newly created .platform/nginx/nginex.conf
below is my custom .platform/nginx/nginex.conf
user nginx;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
worker_processes auto;
worker_rlimit_nofile 32136;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
include conf.d/*.conf;
map $http_upgrade $connection_upgrade {
default "upgrade";
}
server {
listen 80 default_server;
location / {
try_files $uri $uri/ /index.php?$args;
}
location /admin {
try_files $uri $uri/ /admin/index.html?$args;
}
location /thumbnail {
try_files $uri $uri/ /thumbnail/index.php?$args;
}
# Deny direct access to php files in extensions
location /extensions/.+\.php$ {
deny all;
}
# All uploads files (originals) cached for a year
location ~* /uploads/([^/]+)/originals/(.*) {
add_header Cache-Control "max-age=31536000";
}
# Serve php, html and cgi files as text file
location ~* /uploads/.*\.(php|phps|php5|htm|shtml|xhtml|cgi.+)?$ {
add_header Content-Type text/plain;
}
# Deny access to any file starting with .ht,
# including .htaccess and .htpasswd
location ~ /\.ht {
deny all;
}
# pass PHP scripts to FastCGI server
location ~ \.php$ {
fastcgi_pass unix:/var/run/php-fpm/www.sock;
fastcgi_index index.php;
include /etc/nginx/fastcgi.conf;
}
access_log /var/log/nginx/access.log main;
# Include the Elastic Beanstalk generated locations
include conf.d/elasticbeanstalk/*.conf;
}
}
done, when we uploading it, beanstalk will automatically replace our custom nginex.conf with existing nginex.conf. (note: we can add the changes only instead of replacing, but it didn't work at the time I tried)

I have nginx running but it doesn't serve my react build

I am running on ubuntu. I have nginx running on my computer. I get the welcome page for nginx when I go to the my ip address in the browser but I can't see my react app. I just get a 404 error that says nginx on it. My app is a react app built with create-react app. I made the build with npm run build. I created a site in site-available with nano. which looks like
server {
server_name 192.168.43.177;
root home/a/Documents/d/dapp1/mainDapp/client/build/;
index index.html;
location / {
try_files $uri /index.html =404;
}
}
at first I ran
sudo ln -s /etc/nginx/sites-available/sdft /etc/nginx/sites-enabled/sdft
That brings up 404 error page in the browser that says nginx on it. I then ran this command
sudo ln -s /etc/nginx/sites-available/sdft.nginx /etc/nginx/sites-enabled/sdft.nginx
that bring up "This site can’t be reached" when I go to the ip address in my browser
and every time I edit a file I restart the server but there is no mention of nginx on the error page.
systemctl restart nginx
Please help me.
I am adding an another answer.
You want to navigate to the IP (192.168.43.177). Webservers listen to port 80 (by default).
I get the welcome page for nginx when I go to the my ip address.... is it like this?
If yes nginx is working properly. Goto /etc/nginx/sites-available/default and see what is the path of the root.
In the example above, path is /var/www/html. You should copy your build to this path, i.e. your index.html must be here.
You dont have to change anything in the default.
Running on the belief that you have already loaded the index.html directly with your browser ...
I believe nginx looks for .conf files.
Please try renaming your /etc/nginx/sites-available/sdft.nginx to sdft.conf
Then please try setting your symlink to
ln -s /etc/nginx/sites-available/sdft.conf /etc/nginx/sites-enabled/sdft.conf
If your issue remains unresolved then please try
server {
server_name 192.168.43.177;
root home/a/Documents/d/dapp1/mainDapp/client/build/;
index index.html;
location / {
try_files $uri $uri/ =404;
}
}
Noting the change in from index.html to $uri/ location.
Please also validate your system is listening for port 80 and no other services are competing.
Due to reputation limits I can not ask you questions.
Firstly check if all configuration for nginx is passing with:
sudo nginx -t
Is it okay?
sudo ln -s /etc/nginx/sites-available/**sdft** /etc/nginx/sites-enabled/**sdft**
These filename must match.
and also try adding listen 80;, so that it knows what port to listen to.
server {
listen 80; # add this
server_name 192.168.43.177;
root home/a/Documents/d/dapp1/mainDapp/client/build/;
index index.html;
location / {
try_files $uri $uri/ =404;
}
}

Deploy Laravel and React spa

How can I deploy these two together, I don't like the Laravel React preset, I want to separate both, bundle the React app and deploy them together with any web server (apache, nginx...)
EDIT
This is my config for Laravel, but it isn't loading the routes
server {
listen 8000;
server_name 127.0.0.1
root "..\..\Proyecto\Backend\JWT\public";
add_header 'Access-Control-Allow-Origin' '*';
add_header X-Frame-Options "SAMEORIGIN";
add_header X-XSS-Protection "1; mode=block";
add_header X-Content-Type-Options "nosniff";
index index.html index.htm index.php;
charset utf-8;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
error_page 404 /index.php;
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.(?!well-known).* {
deny all;
}
}
You can run them separately using nginx
you run each on separate ports and use methods (POST/GET) to push/get data
use pm2 (http://pm2.keymetrics.io/) for running React (I recommend it because you can monitor the activity of the react app and if you want to do maintenance you can stop the current app process and run a "under maintenance" app process)
you can read more about running laravel on nginx here (https://www.digitalocean.com/community/tutorials/how-to-deploy-a-laravel-application-with-nginx-on-ubuntu-16-04)
as for running react without pm2, you have to build the project yarn build and tell nginx that the file you want to load is the index.html inside of the build file
assuming that you are using an ubuntu server and you uploaded your code to github or gitlab
server {
listen 50;
root /var/www/[Your repo name]/build;
server_name [your.domain.com] [your other domain if you want to];
index index.html;
location / {
try_files $uri /index.html;
}
}
you write this inside of your nginx configuration along with the laravel configuration on a separate port
hope my answer helped a bit
This was proving to be very tricky and it took me at least 3 days to put everything together. Here is what you have to do.
Run
npm run build in the react project.
Copy the contents of the build folder to the server
scp react_project/build/* <server name or ip>:/var/www/html/react
Change the ownership of the project folders to the user www-data or add your user id to the group www-data.
Now. set up the Laravel project in a different directory (in /var/www/html/laravel, for example).
Set up the database, environment variables.
Run
php artisan key:generate
php artisan config:clear
php artisan config:cache
Now, proceed with nginx configuration. Create 2 configs for react and laravel projects as given below. Make sure that the listen ports are different for both projects.
Create configuration files for react and laravel projects under /etc/nginx/sites-available
Create symlinks to the created configs under /etc/nginx/sites-enabled as given below
sudo ln -s /etc/nginx/sites-available/react_conf /etc/nginx/sites-enabled/react_conf
sudo ln -s /etc/nginx/sites-available/laravel_conf /etc/nginx/sites-enabled/laravel_conf
And for the contents,
react_conf:
server {
listen 80;
server_name <server_ip or hostname>;
charset utf-8;
root /var/www/html/react;
index index.html index.htm;
# Always serve index.html for any request
location / {
root /var/www/html/react;
try_files $uri /index.html;
}
error_log /var/log/nginx/react-app-error.log;
access_log /var/log/nginx/react-app-access.log;
}
laravel_conf:
server {
listen 90;
server_name <server ip or hostname>;
charset utf-8;
root /var/www/html/laravel/public;
add_header X-Frame-Options "SAMEORIGIN";
add_header X-XSS-Protection "1; mode=block";
add_header X-Content-Type-Options "nosniff";
index index.php index.html index.htm;
# Always serve index.html for any request
location /api {
try_files $uri $uri/ /index.php?$query_string;
}
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
error_page 404 /index.php;
location ~ \.php$ {
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.(?!well-known).* {
deny all;
}
error_log /var/log/nginx/laravel-app-error.log;
access_log /var/log/nginx/laravel-app-access.log;
}
Now, delete the default config present in /etc/nginx/sites-enabled
Also, verify that /etc/nginx/nginx.conf contains the following include directive where the server configs are expected(under http)
include /etc/nginx/sites-enabled/*;
Verify that the config is fine by running
sudo nginx -t
Restart the server
sudo service nginx restart
Now, you should be up and running.
You can approach it by two ways .
First one is when the you are creating react-app in different folder than the laravel project folder . In such case just deploy laravel app and react app in two different url .
The second condition is when the react-app is inside the laravel app . In such case build the react project and put the dist folder in views folder of the laravel project . So in routes/web.php add this
//Used for handling the html file of react project
View::addExtension('html', 'php');
Route::get('/{any}', function () {
//path to dist folder index.html inside the views directory
return view('build/index');
})->where('any', '.*');
Laravel will not server the required js and css file from inside the views folder . So you need copy and paste all the content of the dist folder to public folder of the laravel project . No need to copy paste index.html file but other file need to placed in the pubic folder .
After that visit the root url of the laravel project in the browser the react app should be working

Running a C FastCGI script using NGINX

This should be my last question regarding FastCGI and NGINX (I've asked way too many already), but I had a question regarding running C FastCGI scripts on a web server, specifically NGINX. So right now this is how my nginx.conf file looks like:
user nobody nobody;
events {
worker_connections 1024;
}
http {
server {
listen 80;
server_name localhost;
location / {
root /nginx/html;
index index.html index.htm new.html;
autoindex on;
fastcgi_pass 127.0.0.1:8000;
}
}
}
I have a simple C FastCGI script that prints out Hello World. I know that in order to run this script, I first have to compile the C script which would result in a binary. I then execute this binary using spawn-fcgi -p 8000 -n <binary> or cgi-fcgi -start -connect localhost:8000 ./<binary>. I have had success doing this and displaying the correct results. However, when I do this, the CGI script is the only thing that is being displayed on the web server. I cannot go to any .html page or any other page for that matter. Even if I type in a random extension which should result in a 404 Page not Found error, the CGI script is being displayed. Basically I'm trying to have a index.html be the home page and then when the user clicks on a button, the user is taken to a new page that displays the C CGI script.
Is this possible? If so, how could I do it? I have spent hours trying to find a solution online but have had no success. Please let me know if the question is too vague/unclear or if you need more information! Thank you!
There are two possibilities that I can think of. You can assign a URI to your CGI program and use that to access it. Or you can send any invalid URI to your CGI program.
In the first case, you could use:
root /nginx/html;
index index.html index.htm new.html;
location / {
try_files $uri $uri/ =404;
}
location /api {
fastcgi_pass 127.0.0.1:8000;
}
So, any URI beginning with /api will be sent to the CGI program. Other URIs will be served by nginx, unless not found, in which case a 404 response will be returned.
In the second case, you could use:
root /nginx/html;
index index.html index.htm new.html;
location / {
try_files $uri $uri/ #api;
}
location #api {
fastcgi_pass 127.0.0.1:8000;
}
So any URI that does not exist will be sent to the CGI program.
See this document for the try_files directive, and this document for the location directive.

nginx 301 from www to mobile site causes 404

I'm getting a 404 for the root of my mobile site. My browser detection code looks for a mobile user-aget, sets the vary header, and 301s to the mobile site.
Here is the main site config
server {
listen 80;
server_name www.mydomain.com;
location / {
if ( $is_mobile) {
add_header Vary "User-Agent";
return 301 $scheme://m.mydomain.com$request_uri;
}
}
Here is the mobile site config
server {
listen 80;
server_name m.mydomain.com;
root /var/www/mobile;
index index.html;
location / {
try_files $uri $uri/ #dynamic;
}
location #dynamic {
rewrite ^/(.*)$ /index.html last;
}
}
I'm using the FireFox Override User Agent extension to test. If I go to www.mydomain.com the app loads properly. However, when I switch to a mobile browser Nginx 404s.
Nginx 200s for pages entered manually -
http://m.mydomain.com/index.html
http://m.mydomain.com/about.html
http://m.mydomain.com/pricing.html
Since both index and root are set shouldn't the site point http://m.mydomain.com/ to http://m.mydomain.com/index.html?
If not what is the best standardized approach to get this working?
UPDATE: Added config for mobile detection
Here is the config I use in the main nginx.conf file for mobile detection
map $http_user_agent $is_desktop {
default 0;
~*linux.*android|windows\s+(?:ce|phone) 0; # exceptions to the rule
~*spider|crawl|slurp|bot 1; # bots
~*windows|linux|os\s+x\s*[\d\._]+|solaris|bsd 1; # OSes
}
map $is_desktop $is_mobile {
1 0;
0 1;
}
See nothing wrong. I tried your config file in my devbox and it worked: the request was redirected to m.mydomain.com and the index.html was served.
So it could be something else caused the issue. How did you set $is_mobile? Maybe setting $is_mobile has some side effect and that if block in your question is not picked by nginx to serve the mobile request. Your mobile request went into another location block that doesn't know how to handle it.
In sites-available there was a file named 1 which was symlinked to site-enabled. Not sure how it got there but I unlinked and deleted it, restarted Nginx, and / now resolves to index.html properly. Very odd.

Resources