How to set different context path for React app in NGINX - nginx

I have deployed (using Kubernetes/helm charts) React app to http://localhost:30111/ using embedded NGINX. This single page application requires some NGINX rewrite rules for deep linking of static sources. There are following NGINX rules:
server {
server_name _;
gzip on;
gzip_static on;
location / {
try_files $uri /index.html;
}
}
Everything works fine and application is listening on http://localhost:30111/ as expected.
What I need to do: I need to use new context path for this application e.g. http://localhost:30111/myapp instead of root context path http://localhost:30111/.
How should I write proxy_pass/rewrite rules in NGINX to work on new context path?

Even though its late, i will provide my answer which may help someone later.
As a first step move all the files in the build folder to a sub directory which is same name of context path.
build/myapp/...All static files will be placed here.
Add your context path in package.json file
"homepage": "/myapp" -- this will help set process.env.PUBLIC_URL to /myapp
Add Basename in the BrowserRouter
<BrowserRouter basename={process.env.PUBLIC_URL}>
<Suspense fallback={<Loader />}>
// ... app routes...
</Suspense>
</BrowserRouter>
Change in your nginx configuration to use files from your sub directory
location / {
return 301 /myapp;
}
location ~ ^/myapp {
try_files $uri /myapp/index.html;
You are done. Deploy your application and see Ui redirects directly to your context path and serving the files.

Related

How to route the custom URL path using nginx server?

I am building the angular app and want to route the URL to http://localhost/sample/AngularApp/. but don't want to give the entire URL in the browser. I will give the only localhost. In angular, while building the dist we are using the command ng build --base-href=/sample/AngularApp/ and created the folder structure /sample/AngularApp in Nginx mentioned path /usr/share/nginx/html.so while accessing the application still, we are giving the entire path in browser. so, How could I resolve this issue and how to configure that path in nginx.conf file to autoroute the URL?
I would go with standard configuration:
server {
listen 80;
listen [::]:80;
root /var/www/html/YourAngularApp/dist;
index index.html index.htm;
location / {
try_files $uri $uri/ /index.html;
}
}
This gives opportunity to serve other files (if found) or fall back to index.html located at the project's root.
Now, if you have everything in place you should build your project with
ng build --base-href=/
--base-href=/ can be omitted as it defaults to /. Please have a look at Angular docs about using --base-href and --deploy-url

nginx how to serve pictures or media files?

I'm having issues serving pictures with nginx. I originally started with a Django project and I wanted to serve the user uploaded media files via nginx but I wasn't able to get it working no matter what I tried.
So, I've make a second temporary droplet/server and am trying a bare bones setup with no Django project, just Nginx, to see if I can get it to simply serve an index and a couple pictures in a folder called 'media'. Here is the server block:
server {
listen 80;
listen [::]:80;
root /var/www/example.com/html;
index index.html;
server_name 159.89.141.121;
location / {
try_files $uri $uri/ =404;
}
location /media/ {
root /var/www/example.com/media;
}
}
Now, the index.html is served fine but if I try to go to 159.89.141.121/media/testpic.jpg it still doesn't work and returns a 404 error. I'm at a complete loss here, I've tried using alias instead of root, I've tried changing the folder structure and setting all permissions to 777 and changing folder and file ownership, permissions shouldn't be a problem because the index.html works fine with the same permissions; I just cant seem to figure out what I'm doing wrong. The picture is in the folder but nothing I try allows me to access it via the uri. Are there any obvious problems with my server block?
Well I decided to read the documentation and realized that the location block adds to the root directory specified..
So the pathing of
`location /media/ {
root /var/www/example.com/media;
}`
would end up routing example.com/media/testpic.jpg to /var/www/example.com/media/media/testpic.jpg
I've changed the location block to look like this
location /images/ {
root /var/www/example.com/media;
}
and it will now route example.com/images/testpic.jpg to /var/www/example.com/media/images/testpic.jpg
I'm not sure why it didn't work when I tried the alias directive, though...

NGINX server configuration for static apps

I have a website which is an app containing other apps.
In the main app, I'll load the other apps with iframes.
The main app is served at the domain root: http://example.com
The other apps are served under the /apps path:
http://example.com/apps/app-a
http://example.com/apps/app-b
Also the apps are developed in Polymer so there is client side navigation. I make sure the /apps path is not used on the client side to hit the NGINX server correctly but it also means that for url such as http://example.com/view1 it should redirect to the index.html of the main app. And for url like http://example.com/apps/app-b/view1 it should redirect to the index.html of app-b.
I'm trying to configure NGINX to serve those static apps.
server {
listen 80;
server_name example.com;
root /var/www/example.com/main-app;
index index.html;
try_files $uri $uri/index.html /index.html;
location ~ /apps/([a-z-]+) {
alias /var/www/example.com/apps/$1;
}
}
With the above config I have the main app working with the correct redirection to the index.html for the /view1 path for example.
But I have a 403 forbidden error for the sub apps.
directory index of "/var/www/example.com/apps/app-b" is forbidden,
client: 127.0.0.1, server: example.com, request: "GET /apps/app-b/
I've tried other configurations with no success (infinite redirection leading to /index.html/index.html/index.html...).
I'm sure about the filesystem permissions all directories are 755 and files are 644.
I don't know why it is trying to do a directory index.
Any help is appreciated.
When using alias with a regular expression location, you need to build the entire path to the file. Currently, you are only capturing the second path element.
Having said that, you do not actually need to use an alias here, as the root directive can be use instead.
location /apps/ {
root /var/www/example.com;
}
See this document for details.

Setup nginx to serve static html web pages based on url

Id like to serve to different websites based on url user enter.
For example if user will go to
my-domain.pl/ the content will be served from desktop folder
my-domain.pl/desktop the content will be served from desktop folder
my-domain.pl/mobile the content will be served from mobile folder
root
|
|---mobile
| |--- assets
| |---js,css,img
|
|---desktop
|--- assets
|---js,css,img
I tried that in nginx setup file:
server {
root /desktop
location /mobile {
root /mobile
}
location /desktop{
root /desktop
}
}
but it works only for / path, remaining paths return 404
I tried to add try_files $uri index.html but it seems that it returns index.html files for all request for this location e.g. it returns index.html file instead javascript too.
I am absolutely new in setting up nginx so any help will be appreciated.
You need to make use of the alias directive instead of root: see documentation.
server {
root /;
location /desktop {
}
location /mobile {
alias /mobile;
}
}
(don't forget trailing semicolons)
You should avoid specifying root inside location blocks (cf. nginx pitfalls).
Have you tried the following:
have only one root
use rewrite to serve /desktop by default
The config would look like:
server {
root /;
## this should take care of the redirect to /desktop by default:
location = / {
rewrite ^ /desktop/ redirect;
}
## this one below probably doesn't work:
# rewrite ^/$ /desktop/;
}
P.S. I have no access to a machine with nginx right now so I'm not able to check the rewrite syntax. See also this answer.

Nginx root location

On our server we have the /var/www/root/html/web/ directory that contains all the code, and a /var/www/root/html/web/front/ that contains our static frontend code.
Our frontend communicates with the code only via REST API calls, which have the /api/ prefix, so all the calls will be accessible via ourdomain.com/api/products/ , ourdomain.com/api/products/45 and so on. We also have an admin running there, on ourdomain.com/admin
When we want to see the actual frontend, we have to go to ourdomain.com/front in the browser, which is of course not what we want.
We have, among other stuff, this in our config:
root /var/www/html/web;
index index.php index.html;
location /front {
# some magic to make sure the /front folder will not be parsed
index nothing_will_match;
autoindex on;
}
However, what we wish is that if you go to ourdomain.com it will load /var/www/html/web/front/ folder as root, and if you go to ourdomain.com/api/* or ourdomain.com/admin/* it will load the /var/www/html/web/ as root. Is that possible?
NOTE: the /var/www/html/web/front/ folder can be moved somewhere else if needed, to /var/www/html/front/ for example
What you need is the alias directive (instead of root) to rewrite the URI:
root /var/www/html/web/front;
index index.php index.html;
location / {
}
location /api {
alias /var/www/html/web/;
}
location /admin {
alias /var/www/html/web/;
}

Resources