Nginx: Unable to run multiple site in single URL - nginx

In My application i have 3 sites - User frontend , Admin panel and Splash page .I want to run User frontend in localhost:8080 , Admin panel in localhost:8080/admin and splash in localhost:8080/splash
To run this all i used the following configuration:
server {
listen 8080;
server_name localhost;
root /home/ajit/git/univisior;
location / {
alias /home/ajit/git/univisior/FrontEnd/dist/;
index index.html;
try_files $uri $uri/ /index.html;
}
location /admin{
alias /home/ajit/git/univisior/admin/dist/;
index index.html;
try_files $uri $uri/ index.html;
}
location /splash {
alias /home/ajit/git/univisior/Splash/dist/;
index index.html;
try_files $uri $uri/ /index.html;
}
location /api {
proxy_pass http://localhost:3000/api;
}
}
The issue with this configuration is this
When i am opening admin site (localhost:8080/admin) i am getting error
VM189:1 Uncaught SyntaxError: Unexpected token <
but when i am running admin site with single site configuration it works fine , nginx config is pasted below:
server {
listen 9010;
server_name localhost;
root /home/ajit/git/univisior/admin/dist;
index index.html index.htm;
location / {
index index.html;
try_files $uri $uri/ /index.html;
}
location /api {
proxy_pass http://localhost:3000/api;
}
location /static {
alias /opt/univisor;
}
}
With splash page i am unable to get images.splash also works fine if i am running this with single site configuration just like admin.
User front is working fine but i am facing issue with admin and splash.can anybody help me out of this problem.
Thanks

Remove the trailing / from your alias directives:
location / {
alias /home/ajit/git/univisior/FrontEnd/dist/;
index index.html;
try_files $uri $uri/ /index.html;
}
location /admin{
# alias /home/ajit/git/univisior/admin/dist/;
alias /home/ajit/git/univisior/admin/dist;
# index index.html;
# try_files $uri $uri/ index.html;
try_files $url $uri/;
}
location /splash {
# alias /home/ajit/git/univisior/Splash/dist/;
alias /home/ajit/git/univisior/Splash/dist;
# index index.html;
# try_files $uri $uri/ /index.html;
try_files $uri $uri/;
}

I was able to figure out the issue. The issue in both admin and splash page were same. The path for images and scripts files in index.html was written as
<img src="../images/logo.png" alt="logo">
.
.
<script src="../bower_components/jquery/dist/jquery.js"></script>
<script src="../bower_components/angular/angular.js"></script>
When running index file with single site nginx configuration (admin site configuration pasted above in question) both admin and splash works fine. But when running both of them in configuration like this
.
.
location / {
alias /home/ajit/git/univisior/FrontEnd/dist/;
index index.html;
try_files $uri $uri/ /index.html;
}
location /admin{
alias /home/ajit/git/univisior/admin/dist/;
index index.html;
try_files $uri $uri/ index.html;
}
location /splash {
alias /home/ajit/git/univisior/Splash/dist/;
index index.html;
try_files $uri $uri/ /index.html;
}
.
.
makes issue as <img src="../images/logo.png" alt="logo"> will look for url localhost:8080/images/logo.png instead of localhost:8080/admin/images/logo.png same applies for scripts because of this browser is not able to get scripts/images.
Inorder to solve this i replaced ../ from index.html to have a final index.html like this.
<img src="images/logo.png" alt="logo">
.
.
<script src="bower_components/jquery/dist/jquery.js"></script>
<script src="bower_components/angular/angular.js"></script>

Related

Nginx shows 404 error for sub link and paths

I have using nginx for laravel project. When I use the domain name it shows the home page. But When I click any one of the link it does not work. And shows 404 Not Found error.
http://www.myhomepage.com // working
http://www.myhomepage.com/about // not working
I have using the following configuration.
server {
listen 80;
root /var/www/abc-company-website/public;
index index.php index.html index.htm index.nginx-debian.html;
server_name myhomepage.com localhost;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
}
location ~ /\.ht {
deny all;
}
}
You probably want to adjust the location / block to pass the query string, as per the documentation:
location / {
try_files $uri $uri/ /index.php?$query_string;
}
Laravel 5.8 Docs - Installation - Pretty URLs - Nginx

nginx match location after prefix

The problem is that nginx is matching paths correctly on www.example.com/en/
or www.example.com/pl/ but not www.example.com/en/something/. If I go to www.example.com/en/something/ then I get "Welcome to nginx!" page.
When I visit www.example.com/en/ then do action that redirects to www.example.com/en/something/ - this scenario works.
I tried locations: '/en', '^~ /en'.
What's going on?
my nginx.conf is looking like this:
server {
index index.html index.htm index.nginx-debian.html;
server_name xxx; # managed by Certbot
location / {
root /usr/share/nginx/html/en;
index index.html index.htm;
try_files $uri $uri/ /index.html =404;
}
location /en/ {
root /usr/share/nginx/html;
index index.html index.htm;
try_files $uri $uri/ /index.html =404;
}
location /pl/ {
root /usr/share/nginx/html;
index index.html index.htm;
try_files $uri $uri/ /index.html =404;
}
With your current configuration, you use try_files $uri $uri/ /index.html =404;.
Ignoring the redundant =404 on the end, if the file is not found, the file located at /usr/share/nginx/html/index.html will be returned. And that file probably contains "Welcome to nginx!".
All parameters of the try_files directive are like URIs, and the correct URI for the /en/ index page is /en/index.html.
For example:
index index.html index.htm;
root /usr/share/nginx/html;
location /en/ {
try_files $uri $uri/ /en/index.html;
}
location /pl/ {
try_files $uri $uri/ /pl/index.html;
}
See this document for details.

nginx centos 7 server multiple website

my server ip is 103.107.122.13 and port:5680.
i want two application suppose 103.107.122.13:5680/test1 then it enter var/www/html/test1 website and 103.107.122.13:5680/test2 it enter var/www/html/test2 website using virtual host. please tell me how can i manage this need also any change in nginx.conf file .
if anyone know please help me to solve this problem .
I already write two file inside site-available folder test1.conf file
server {
listen 80;
location /test1{
root /var/www/html/test1;
index index.php;
try_files $uri $uri/ =404;
}
}
and test2.conf file
server {
listen 80;
location /test2{
root /var/www/html/test2;
index index.php;
try_files $uri $uri/ =404;
}
}
Why different files? You should put this to /etc/nginx/nginx.conf:
http {
server {
listen 80;
location /test1{
root /var/www/html/test1;
index index.php;
try_files $uri $uri/ =404;
}
location /test2{
root /var/www/html/test2;
index index.php;
try_files $uri $uri/ =404;
}
}
}

Nginx configuration for single page app and nested directories

I have a directory/files structure such as:
root/
a/
utils.js
b/
assets/
styles.css
app.js
index.html
And I want to configure nginx to serve files from a directory directly if exist and have single page app in directory b (if file in path exists the it wil be served directly, nd if not the fallback will end up at index.htm file.
For example:
myapp.com/a/utils.js will return that file.
myapp.com/b/ or myapp.com/b/foo will display index.html
myapp.com/b/assets/style.css will return directly css file
I tries multiple different configurations and non had worke so far. For exampe the simplest:
server {
listen 80;
root /root;
index index.html;
location / {
try_files $uri $uri/ /index.html =404;
}
}
I also tries something to serve different directories:
server {
listen 80;
root /root;
index index.html;
location /a {
try_files $uri $uri/ =404;
}
location /b {
try_files $uri $uri/ /index.html =404;
}
}
I tried to define different roots as well:
server {
listen 80;
index index.html;
location /a {
root /root/a;
try_files $uri $uri/ =404;
}
location /b {
root /root/b;
try_files $uri $uri/ /index.html =404;
}
}
Nginx seems to ignore existing files and ends up returning 404 page at all times. When I try to access soe existing file directly it gets redirected to / (root) url regardless.
The last parameter of a try_files statement is the default action. There can only be one. Many of your examples have two. See this document for details.
The correct URI for your index.html file is /b/index.html which is what you need to use for the default action of the try_files statement.
This should meet your requirements:
location / {
try_files $uri $uri/ /b/index.html;
}
You do not state what should happen with the URI /a/foo. In the above case, it would also return index.html. If you need it to return a 404 response, you would use:
location / {
try_files $uri $uri/ =404;
}
location /b {
try_files $uri $uri/ /b/index.html;
}
See this document for more.

How to servers my homepage with and HTML file and all others paths/urls with a PHP file

What I'm trying to achieve is if someone visit my home/index page, I need to server my index.html file. But, if it's another URL/path pass the request to my index.php file (I'm using Symfony).
I follow this example, but is not working. It's always serving my PHP file.
server
{
listen 80;
server_name mysite.com;
root /path/to/my/web;
index index.html index.php;
location = /index.html
{
try_files $uri /index.html?$args;
}
location /
{
try_files $uri /index.php?$args;
}
}
I will appreciate any help or guidance you can share with me.
This finally worked for me:
server
{
listen 80;
server_name mysite.com;
root /path/to/my/web;
index index.html index.php;
location = /
{
try_files $uri /index.html?$args;
}
location /
{
try_files $uri /index.php?$args;
}
}

Resources