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; }
Related
I would like to to route requests based on a path to two different Angular applications. So when i request http://example.com/admin is routes to one app http://example.com/client routes to the second app. I have the following config but all requests are always sent to the nginx default page. Configuration is as follows:
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
location /admin {
root /home/ubuntu/apps/admin/;
index index.html;
try_files $uri $uri/ /index.html?$args;
}
location /client {
root /home/ubuntu/apps/client;
index index.html;
try_files $uri $uri/ /index.html?$args;
}
}
No other confs are in /etc/nginx/sites-enabled and nginx.conf is default post install on Ubuntu. Any help is appreciated.
You were using the wrong value for the root directive. In both locations the correct value for the root directive is /home/ubuntu/apps, which means you can simplify the configuration by using just one root directive by moving it into the server block.
Of course you can use the alias directive - but as the manual states :
When location matches the last part of the directive’s value ... it is better to use the root directive instead.
The other problem is that your try_files statements are pointing to the wrong index.html file.
For example:
server {
listen 80 default_server;
listen [::]:80 default_server;
root /home/ubuntu/apps;
location /admin {
try_files $uri $uri/ /admin/index.html;
}
location /client {
try_files $uri $uri/ /client/index.html;
}
}
Note that server_name _; is not necessary - see the Server Names document.
Also, index index.html; is not necessary being the default value for the index directive.
It appears that you cannot use multiple root directives but instead need to use alias (Configure nginx with multiple locations with different root folders on subdomain). With that, I would still get 404s until I took off $args from the index.html. After that everything worked fine (don't ask how long it took to figure that out). Working config:
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
index index.html;
location /admin {
alias /home/ubuntu/apps/admin;
try_files $uri /index.html =404;
}
location /client {
alias /home/ubuntu/apps/client;
try_files $uri /index.html =404;
}
}
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;
}
}
Trying to configure my site to change root directory when there is a cookie var called "developer". This is on a Debian server.
This is my current site config file:
server {
listen 80 default_server;
listen [::]:80 default_server;
root /sites/live/;
if ($http_cookie ~ 'developer') {
root /sites/dev/;
}
index index.php index.html index.htm index.nginx-debian.html;
server_name _;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
try_files $uri =404;
include /etc/nginx/fastcgi.conf;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}
}
But that gives me this error:
nginx: [emerg] "root" directive is not allowed here
What should I be doing? Thanks!
add root inside the location and the condition what you want
following is he my configuration working fine
location / {
root /var/www/myside;
index index.php;
try_files $uri $uri/ /index.php$is_args$args;
}
if that not work use rewrite rule
I have an application with many urls like this:
dashboard.app.mycooldomain.com
subdomain-1.app.mycooldomain.com
subdomain-1.app.mycooldomain.com
subdomain-3.app.mycooldomain.com
subdomain-n.app.mycooldomain.com
and nginx config
server {
listen 5001 default;
listen [::]:5001;
server_name *.$hostname;
location / {
alias /usr/share/nginx/html/home/;
index index.html;
try_files $uri $uri/ index.html =404;
}
}
server {
listen 5001;
listen [::]:5001;
server_name dashboard.*$hostname;
location / {
alias /usr/share/nginx/html/dashboard/;
index index.html;
try_files $uri $uri/ index.html =404;
}
}
I expect when I visit dashboard.app.mycooldomain.com or dashboard.app.localhost nginx should serve all static file in /usr/share/nginx/html/home/ and when I subdomain-1.app.mycooldomain.com or subdomain-1.app.localhost or *.app.mycooldomain.com nginx should serve all static file in /usr/share/nginx/html/dashboard/. But now it does not work. How to write a config file correctly?
Create subdomains files in /etc/nginx/sites-enabled for all the required/different hosts/url, this way it would be much easier to manage the domains.
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;
}