Redirect with parameter to same url - nginx

I have a server with nginx and I have to make that whenever they write a domain url it is redirected to the same domain but with a parameter, that is:
http: // my-site => http: // my-site / blue
but when i use rewrite:
server {
listen 80;
server_name my-site.org;
index index.html index.htm;
root /var/www/my-site.com;
location / {
try_files $uri $uri/ /index.html;
rewrite ^/ /blue;
}
}
Or use return:
server {
listen 80;
return 301 https://$host/blue;
server_name my-site.org;
index index.html index.htm;
root /var/www/my-site.com;
location / {
try_files $uri $uri/ /index.html;
}
}
I receive the error 500 internal server error
In front use angular 6

Related

How to configure multiple react projects using nginx?

I have 3 different React projects, pointing to the same IP address with different ports.
Routing works accurately for the first project(default project)
For the other 2, routing works fine if I'm navigating from the very first page of the website.
For an instance, if I'm at some.ip:3000 then I click something and now, I'm at some.ip:3000/page, it works fine
but if I try some.ip:3000/page directly, 404 page is returned.
Following is the nginx configuration - /etc/nginx/sites-available/default
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name <private-IP>;
root /var/www/<project1>;
index index.html index.htm;
location / {
try_files $uri $uri/ /index.html =404;
}
}
server {
listen 3000;
listen [::]:3000;
server_name <private-IP>;
root /var/www/<project2>;
index index.html index.htm;
location / {
try_files $uri $uri/ =404;
}
}
server {
listen 8000;
listen [::]:8000;
server_name <private-IP>;
root /var/www/<project3>;
index index.html index.htm;
location / {
try_files $uri $uri/ /index.html =404;
}
}

Multiple roots nginx

Is it possible to have a suburl that point to a different root? For example:
www.domain.com/ -> /home/ubuntu/project1
www.domain.com/project2 -> /home/ubuntu/project2
I have this configuration at this moment but I'm getting a 404 when resolving domain.com/project2
server {
listen 80;
server_name domain.com;
root /home/ubuntu/project1;
location /project2 {
root /home/ubuntu/project2;
index index.html;
}
location / {
try_files $uri $uri/ /index.html;
}
}
It's because nginx will append the uri to root directive.
In your example config, accessing domain.com/project2 would try to look for a file named project2 in /home/ubuntu/project2 which is not found and return 404.
To solve your problem, try using alias directives.
server {
listen 80;
server_name domain.com;
root /home/ubuntu/project1;
location /project2 {
alias /home/ubuntu/project2;
index index.html;
}
location / {
try_files $uri $uri/ /index.html;
}
}

Content linked to domain is not working but with subdomain it is working on nginx

I have created several subdomain so that i can serve the subdomain related specific content. But the problem i am facing is that when i am trying to access the content related directly to my domain am not getting anything in return in nginx. Below is my configuration file
http {
include mime.types;
default_type application/octet-stream;
server {
listen 80;
server_name example.com www.example.com;
root /usr/share/nginx/myfile/dev/web;
index index.html index.htm;
location / {
try_files $uri $uri/ /index.html;
}
}
server {
listen 80;
server_name dev.example.com;
root /usr/share/nginx/myfile/dev/mobile;
index index.html index.htm;
location / {
try_files $uri $uri/ /index.html;
}
}
server {
listen 80;
server_name qa-crm.example.com;
root /usr/share/nginx/myfile/qa-crm;
index index.html index.htm;
location / {
try_files $uri $uri/ /index.html;
}
}
server {
listen 80;
server_name qa.example.com;
root /usr/share/nginx/myfile/qa;
index index.html index.htm;
location / {
try_files $uri $uri/ /index.html;
}
}
}
All server block are serving perfectly except the first one which contains the main domain name i.e. (example.com www.example.com)
No idea what i am doing wrong here. I am new to nginx world

Configuring multiple sites on nginx server

I would like to configure multiple websites on my nginx server. My configuration under /etc/nginx/sites-enabled/default is as below :
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
root /var/www/html/MyWebsite;
location / {
try_files $uri $uri/ =404;
}
location /admin {
alias /var/www/html/MyWebsiteAdmin;
try_files $uri $uri/ =404;
}
}
My website is deployed at www.mywebsite.com. I am able to access www.mywebsite.com but not able to access www.mywebsite.com/admin due to few errors like, it cannot load js and css files eg: www.mywebsite.com/inline.bundle.js, www.mywebsite.com/theme.css etc. So, It is trying to access js and css files from www.mywebsite.com but not from www.mywebsite.com/admin/inline.bundle.js. How can i resolve this issue?
/admin are folder then you not need
location /admin {alias /var/www/html/MyWebsiteAdmin;try_files $uri $uri/ =404;}
now /admin is like www.mywebsite.com/index.php/admin then you have to add rewrite rule of index.php
location / {index index.html index.php;try_files $uri $uri/ #handler;}location #handler { rewrite / /index.php; }

nginx how to configure route to html file?

I have the nginx config:
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
root /var/www/site/public;
index main.html;
server_name localhost;
location / {
try_files $uri $uri/ =404;
}
At root directory I also have html files:page1.html, page2.html, page3.html.
I would like to configure route mysite.com/services/page1 to file page1.html. etc. How can I do it?
I tried it:
location = /services/page1 { try_files /page1.html;}
But it doesn't work.
If you want to rewrite url only if the file doesn't exist you can use named location in try_files directive.
location /services {
try_files $uri $uri/ #service_pages;
}
location #service_pages {
rewrite ^/services/page([1-3]).html /page$1.html;
}

Resources