setting up nginx and two sails app on a server - angularjs

I have two different application one acts as a backend panel whereas one app is client facing site.
I have set the client site on the default 80 port and backend panel app is on say port number 8000.
I am facing issue when i try to bind wwww.abc.com:8000 to www.abc.com/admin. On opening of the link, text do gets displayed but css and js is not getting loaded. I found in console that its says CSS and javascript files are not found. Also, the address it specifies for css and javascript is of the root folder and not of the backend panel folder.
Here is the code I have written to setup nginx to bind the route is here
server {
listen 80;
server_name abc.com
include /etc/nginx/mime.types;
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $http_host;
proxy_pass http://127.0.0.1:1337;
}
location ^~ /admin {
root /root/workspace/lexcarts-admin-panel;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://127.0.0.1:8000;
proxy_redirect on;
}
}
Client app is accessible from default www.abc.com .
Please guide me where i am going wrong.
TIA

Related

How to route URL path to private directory in nginx

I am trying to make it so that a request at path https://example.com/projects/pixelstacker/demo will serve static files that are located at /services/pixelstacker.web.net/wwwroot/dist. I want it to be located there so that I can keep related files closer together in the deployment system and file trees. It just makes it easier to manage. I'm having troubles getting this done in plesk because plesk does not allow you to make custom directives at the root / level for nginx.
The nginx configuration for my .net core app looks like this:
location /projects/pixelstacker/ {
proxy_pass http://localhost:5005/;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection keep-alive;
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Real-IP $remote_addr; # remote IP addr again?
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; # remote IP addr
proxy_set_header X-Forwarded-Proto $scheme;
proxy_redirect off;
}
And then my configuration for my react app can do a basic proxy that WILL work, but only if the files for my react project are deployed to a publicly accessible folder. I'm trying to get this to work but for a folder that is located outside of the httpdocs directory instead. The public folder would be located at /httpdocs/projects/pixelstacker.web.react
# React
# https://stackoverflow.com/questions/53207059/react-nginx-routing-to-subdirectory
location ^~ /projects/pixelstacker/demo {
alias /var/www/vhosts/example.com/httpdocs/projects/pixelstacker.web.react;
try_files $uri $uri/ /projects/pixelstacker.web.react/index.html;
}
Any tips or advice? Thanks!

Problem hosting react app at sub route of website - multiple websites one IP address NGINX

I am new to NGINX so this may be a simple fix, but i cant find any great documentation about what i am trying to do here.
So I have a website which is hosted on an ubuntu aws server by nginx and pm2. my problem is that I want to serve the website at www.mysite.com and the react app at www.mywebsite.com/app. this doesnt seem like it should be the hardest thing to do. However I have been having problems and cannot get it to work as i would like. (part of this is because i also wish to host an API server at www.mywebsite.com/api but that is a problem for after this). I was able to first just host the react app at www.mysite.com with the following nginx config:
server {
listen 80;
server_name 18.191.56.251;
root /home/ubuntu/app/src;
location / {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_max_temp_file_size 0;
proxy_pass http://127.0.0.1:3000/;
proxy_redirect off;
proxy_read_timeout 240s;
}
}
But when i switch it to the sub route by altering the config as so:
server {
listen 80;
server_name x.x.x.x;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_redirect off;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_redirect off;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_connect_timeout 600;
proxy_send_timeout 600;
proxy_read_timeout 600;
send_timeout 600;
location /status {
return 200;
}
location /app {
proxy_pass http://127.0.0.1:3000;
}
location / {
proxy_pass http://...:3011/;
}
}
Then my base route www.mywebsite.com for the static website works fine, but www.mywebsite.com/app does not and "loads" a blank page but if you inspect you see that the app is unable to load some resources:
bundle.js:1
Failed to load resource: the server responded with a status of 404 (Not Found)
0.chunk.js:1
Failed to load resource: the server responded with a status of 404 (Not Found)
main.chunk.js:1
Failed to load resource: the server responded with a status of 404 (Not Found)
EDIT more research:
If i examine the request for the resource i see its url is www.mywebsite.com/static/js/XXX.js but the static folder is at ~/app/build/static. is this a nginx or a pm2 problem with hosting the static files?
Any help is appreciated, thanks
So I would still love a different result then this but i figured out a workaround for this, i added the following after location /app:
location /static {
proxy_pass http://127.0.0.1:3000;
}
i dont love this solution because if i end up having static content as part of other projects then it will not work, but somehow all the resources for my website are packaged within the correct proxy at /. so currently it is all working.

How to configure NGINX to serve a JSON API and a React.js on the same box

I have been working on JSON API written in Elixir using the phoenix framework, and I successfully deployed yesterday. However, the API is not that useful with out a web frontend, which I wrote one as well using React.js.
I'm deploying the phoenix API using a combination of distillery and gatling, and everything appears to be working when I test it using Postman.
Then I edited the nginx configuration file to loook like the following,
/etc/nginx/sites-available/kegcopr_api
server {
listen 80;
server_name kegcopr.chrisrjones.com;
root /opt/Code/react/kegcopr-frontend/build;
index index.html index.htm;
access_log /home/deploy/deployments/kegcopr_api/nginx.access.log;
error_log /home/deploy/deployments/kegcopr_api/nginx.error.log;
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://localhost:33725;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
}
I then created a .env file in the root of React.js project with the following line,
REACT_APP_API_URL=http://localhost:33725/api
Any help on how to get this configured would greatly be appreciated.
UPDATE
I changed the .env file to,
REACT_APP_API_URL=http://kegcopr.chrisrjones.com/api
and ran the below command,
npm run build
but I am still not seeing the React.js frontend display in the browser.
You have set the root directory, but you're sending all requests unconditionally to localhost:33725. If you want to serve static files from the root directory and pass all other requests to the proxy_pass, you can use try_files like this:
location / {
try_files $uri #proxy;
}
location #proxy {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://localhost:33725;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
try_files will try to locate the file within root with the same name as the URL, and if it fails to find a file, it'll pass the request to #proxy, which will pass it to the Phoenix app running on localhost:33725.

I have a proxied node app on port 9000 working well, but after login the port shows up why?

My angular-fullstack express app is working well. It is served on port 9000 through nginx using proxy_pass https://example.com:80 -> https://example.com:9000. When a new user looks to https://example.com they are routed to https://example.com/login as expect. However, once they login, whey see the url: https://example.com:9000 on firefox. And on Chrome they see: https://example.com:9000/# ??? why in the world is the port showing up and why is the # appearing?
In case the nginx settings are to blame for the proxy_pass, here they are:
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
proxy_pass https://127.0.0.1:9000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-NginX-Proxy true;
}
Is there a simple way to tell nginx to hide the port on the response, and why does the # appear on Chrome?
Your insight is appreciated!
To address the port number showing up, you might need to set the proxy_redirect directive in your nginx config. As the linked doc states, proxy_redirect
Sets the text that should be changed in the “Location” and “Refresh” header fields of a proxied server response
which is most likely where the port number is being added in.
As for the #, that's application specific - you're going to have to look at your code to figure out why.

Nginx - configure location regex for all static file in sub alias

I've been trying almost one day with no luck to configure nginx as proxy reverse for apache.
I expect that nginx will serve all static file, and the rest is passed to apache.
It's not hard to achieve this if the site resides on the root domain name.
For example, I've successfully set:
www.mydomain.com
location ~* \.(jpg|jpeg|gif|png|css|js|ico|xml|rss|txt)$ {
root /path/to/mysite;
}
location / {
proxy_pass 127.0.0.1:8080;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Real-IP $remote_addr;
}
now the problem arises as I want to configure site within alias
for example:
www.mydomain.com/site1
Does anybody know how to achieve the same purpose?
I tried below configuration doesn't work:
location /site1 {
location ~* \.(jpg|jpeg|gif|png|css|js|ico|xml|rss|txt)$ {
root /path/to/anothersite1;
}
proxy_pass apache:8080;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Real-IP $remote_addr;
}
the problem is that the 'root' directive doesn't replace the /site1 URI.
For example, a request to
www.mydomain.com/site1/folder/pic.jpg
will be handled by opening file:
/path/to/anothersite1/site1/folder/pic.jpg;
instead of:
/path/to/anothersite1/folder/pic.jpg
I really appreciate any help on this.
I've tried to replace 'root' directive with 'alias' but still no luck.
Thanks beforehand,

Resources