My ngix site config file is given below. I want to add context path to my URL
I can access site by http://localhost:8888, but I want to add context path to my site URL like http://localhost:8888/MyApp
server {
listen 8888;
server_name localhost;
location{
root "C:/nginx/Share/dist";
index index.html index.htm;
}
}
Thanks in advance
You need to change the base location for this
server {
listen 8888;
server_name localhost;
location / {
# since we have nothing on root we can redirect to /MyApp/ if we want
return 302 /MyApp;
}
location /MyApp {
root "C:/nginx/Share/dist";
index index.html index.htm;
}
}
Related
I am trying to implement something like that in the nginx conf:
subdomain
sub.domain.com -> Serve html
sub.domain.com/api -> proxy to port 3001
sub.domain.com/viewer -> serve another html
subdomain2
sub2.domain.com -> proxy to port 3000
The only route that doesn't work is the viewer, I get the html from the "location /". All other configurations work well.
I tried to move the viewer to the bottom then to the top and to the middle no matter what it doesn't work.
I use CentOS7. This is the configurations currently in the server:
events {
}
http {
server {
listen 80;
listen [::]:80;
server_name www.sub.domain.com subdomain.com;
location /viewer {
root /opt/viewer/;
try_files $uri /index.html;
index index.html;
}
location / {
root /opt/client-bo/;
try_files $uri /index.html;
index index.html;
}
location /api {
proxy_pass "http://localhost:3001";
}
}
server {
listen 80;
server_name www.sub2.domain.com sub2.domain.com;
listen [::]:80;
location / {
proxy_pass "http://localhost:3000";
}
}
}
Thanks!
If your viewer app located in the /opt/viewer directory and you want it to be available under the /viewer URI prefix, you should use root /opt; for the location /viewer { ... }. Check the difference between root and alias directives.
Next, the very last argument of the try_files directive is treated as the new URI to re-evaluate, so you /index.html being treated as the new URI going to be served with the location / { ... }. You should change that directive to
try_files $uri /viewer/index.html;
I am trying to implement something like that in the nginx conf:
subdomain
sub.domain.com -> Serve html
sub.domain.com/api -> proxy to port 3001
sub.domain.com/viewer -> serve another html
subdomain2
sub2.domain.com -> proxy to port 3000
The only route that doesn't work is the viewer, I get the html from the "location /". All other configurations work well.
I tried to move the viewer to the bottom then to the top and to the middle no matter what it doesn't work.
I use CentOS7. This is the configurations currently in the server:
events {
}
http {
server {
listen 80;
listen [::]:80;
server_name www.sub.domain.com subdomain.com;
location /viewer {
root /opt/viewer/;
try_files $uri /index.html;
index index.html;
}
location / {
root /opt/client-bo/;
try_files $uri /index.html;
index index.html;
}
location /api {
proxy_pass "http://localhost:3001";
}
}
server {
listen 80;
server_name www.sub2.domain.com sub2.domain.com;
listen [::]:80;
location / {
proxy_pass "http://localhost:3000";
}
}
}
Thanks!
If your viewer app located in the /opt/viewer directory and you want it to be available under the /viewer URI prefix, you should use root /opt; for the location /viewer { ... }. Check the difference between root and alias directives.
Next, the very last argument of the try_files directive is treated as the new URI to re-evaluate, so you /index.html being treated as the new URI going to be served with the location / { ... }. You should change that directive to
try_files $uri /viewer/index.html;
I am trying to run 2 applications behind an NGINX server.
one is listening on 3000 (grafana) and one is listening on 9090 (prometheus)
My current server block nginx.conf looks like this:
server {
listen 80;
root /usr/share/nginx/html;
index index.html index.htm;
location / {
proxy_pass http://localhost:3000/;
}
location /prometheus{
proxy_pass http://localhost:9090/;
}
}
Now for Grafana, this works perfectly and everything works in the dashboard.
But when typing Domain/prometheus it still redirects me to grafana instead of Prometheus.
Do I need todo something specific to have it working in this setup that everything besides /prometheus is redirected to grafana?
By default, http://localhost:9090 will be redirected to http://localhost:9090/graph, so the request is being redirected as below:
# the origin request
http://Domain/prometheus
# the request redirect by nginx
http://localhost:9090/
# the request redirect by prometheus
http://Domain/graph
# the request redirect by nginx again
http://localhost:3000/graph
You can check this by using F12 in Chrome.
To fix this, I recommend you separate the domain into two domains:
server {
listen 80;
server_name Domain-Grafana; # Domain for Grafana
root /usr/share/nginx/html;
index index.html index.htm;
location / {
proxy_pass http://localhost:3000/;
}
}
server {
listen 80;
server_name Domain-Prometheus; # Domain for Prometheus
root /usr/share/nginx/html;
index index.html index.htm;
location / {
proxy_pass http://localhost:9090/;
}
}
So I solved it now by doing this:
server {
listen 80;
root /usr/share/nginx/html;
index index.html index.htm;
location /prometheus {
proxy_pass http://localhost:9090/prometheus;
}
location / {
proxy_pass http://localhost:3000/;
}
}
I am trying to run my react app using Nginx.
I created build of my app (name: react-app) and placed it over here /var/www/react-app.
Then I created a conf file /etc/nginx/conf.d/react-app.conf
server {
listen 8081;
server_name localhost;
location / {
root /var/www/react-app;
index index.html index.html;
}
}
There is include /etc/nginx/conf.d/*.conf; inside /etc/nginx/nginx.conf.
Then I ran nginx and opened http://localhost:8081/ in the browser, but result is blank.
How can fix this?
You should not use index and add a try_files:
server {
listen 8081;
server_name localhost;
location / {
root /var/www/react-app;
try_files $uri $uri/ /index.html;
}
}
Is it possible to serve all requests to example.com ( and www.example.co)
from the root folder
location / {
root html;
index index.html index.htm;
}
but all requests to admin.example.com from a another folder 'admin' on the same server ?
The admin.example.com should be configured as a separate server in nginx config, but noone restricts you from pointing its root inside other server's root directory
server {
listen 80;
server_name admin.example.com;
root /some/path/example.com/admin;
location / {
index index.html;
}
}
server {
listen 80;
server_name www.example.com example.com;
root /some/path/example.com;
location / {
index index.html index.htm;
}
}
Use different server directives, this way:
server {
server_name admin.example.com;
root admin;
location / {
index index.html index.htm;
}
}
server {
server_name example.com;
root html;
location / {
index index.html index.htm;
}
}
Also, it is better to use only one root directive at server level, not to repeat it across location blocks (cf pitfalls).