nginx redirect all urls to index.html - reactjs

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.

Related

My website is not loading the same page when i refresh the page

I have deployed multiple reactapps using docker and nginx as reverse proxy.
My Docker file is same for seperate 3 react apps!!
FROM node:16-alpine
WORKDIR /app
COPY package.json ./
COPY package-lock.json ./
COPY ./ ./
RUN npm i
CMD ["npm", "run", "start"]
My nginx.conf
server {
listen 80;
return 301 https://$host$request_uri;
}
server {
listen 443 ssl default_server;
listen [::]:443 ssl default_server;
add_header 'Cache-Control' "public, max-age=31536000";
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
add_header X-Frame-Options "SAMEORIGIN";
add_header X-XSS-Protection "1; mode=block";
add_header X-Content-Type-Options: "nosniff";
ssl_certificate /etc/nginx/conf.d/cert.crt;
ssl_certificate_key /etc/nginx/conf.d/ssl.key;
server_name <my-ip-address>;
location / {
proxy_pass http://172.17.0.5:3000;
#try_files $uri /index.html;
}
location /qae {
proxy_pass http://172.17.0.2:3001;
#try_files $uri /index.html;
}
location /qac {
proxy_pass http://172.17.0.4:3002;
#try_files $uri /index.html;
}
}
This code is running fine and my reactapps are opening on this, but when i login into my website and try reloading my page its redirecting to 404 error page.
When i remove the hashtag for try_files $uri /index.html; iam getting 500 internal server error.
Solution needed : When i refresh the reactapp it has to stay on the same page.
Iam using 3 different images for 3 reactapps and also nginx is seperate image and making proxy_pass for 3 containers.
Hosting react files under nginx should just be a matter of copying all the files into the document root directory. Something like (loosely based on this article):
mkdir -p /var/www/my_site
cd /home/me/react1
npm run build
cp -r ./build/* /var/www/my_site/
mkdir -p /var/www/my_site/qae
cd /home/me/react2
npm run build
cp -r ./build/* /var/www/my_site/qae/
mkdir -p /var/www/my_site/qac
cd /home/me/react3
npm run build
cp -r ./build/* /var/www/my_site/qac/
Then in your nginx conf you would need different try_files rules for each subdirectory so that each one would use the correct index.html. (Loosely based on this answer):
root /var/www/my_site;
location / {
try_files $uri /index.html;
}
location /qae {
try_files $uri /qae/index.html;
}
location /qac {
try_files $uri /qac/index.html;
}

Nginx configuration to cache angular app files

I have the following configuration to run an angular.js app, which works fine.
location / {
root /usr/share/nginx/html;
index index.html;
expires -1;
add_header Pragma "no-cache";
add_header Cache-Control "no-store, no-cache, must-revalidate, post-check=0, pre-check=0";
try_files $uri $uri/ /index.html =404;
}
But when I add cache
location ~* \.(jpg|jpeg|png|gif|swf|svg|ico|mp4|eot|ttf|otf|woff|woff2|css|js)$ {
add_header Cache-Control "max-age=86400, must-revalidate, s-maxage=2592000";
}
The files are not longer accesibles
[error] 5#5: *1 open() "/etc/nginx/html/scripts/vendor-1568b32f3e.js" failed (2: No such file or directory), client: 87.63.75.163, server: localhost, request: "GET /scripts/vendor-1568b32f3e.js HTTP/1.1", host: "myapp", referrer: "http://myapp/auth/login"
Not sure why is trying to get the resources from /etc/nginx/html/ when the root path is /usr/share/nginx/html
You need to make sure that the "root" defined in the server section matches what you want. Or define "root" under your regex location location ~* \.(jpg|jpeg|png|gif|swf|svg|ico|mp4|eot|ttf|otf|woff|woff2|css|js)$. Without a root defined there nginx may be reverting to global definition under the server section.

Cakephp 3 NGINX URL rewriting issue

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.

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.

Nginx single application config

I'm writing an AngularJS single page application using nginx.
I just switched from apache to nginx, but I cant make my config file working.
I'm trying to rewrite everything to index.html to let Angular do the routing.
My nginx.conf is as follow:
server {
index index.html;
location / {
expires -1;
add_header Pragma "no-cache";
add_header Cache-Control "no-store, no-cache, must-revalidate, post-check=0, pre-check=0";
try_files $uri $uri/ /index.html;
}
}
Is it possible to have my nginx.conf file in the project root like my .htaccess did?
You dont want nginx.conf in the project root and its not necessary. Also, you don't want direct changes to nginx.conf, you will instead want specific files for different websites in /etc/nginx/sites-available which you enable with a ln in /etc/nginx/sites-enabled.
As far as the config:
server {
root /var/www/mysite/; #or whereever your site files are
index index.html;
location /{
try_files $uri $uri/ =404;
}
}
You are missing the root portion which tells nginx where the site is located.

Resources