I am currently deploying a ReactJS app to a Nginx server. The backend the application is located in a private network. I am currently using http://my-domain.com/ to access the frontend and from the frontend I access the backend using http://my-domain.com/api/. I am trying to configure Nginx to pass from http://my-domain.com/api/ to http://my-backend-loadbalancer.com/api/.
This is the configuration that I am running from `/etc/nginx/sites-available/my-domain.com
server {
listen 80;
listen [::]:80;
root /var/www/my-domain.com/html;
index index.html index.htm index.nginx-debian.html;
server_name www.my-domain.com my-domain.com;
location / {
try_files $uri $uri/ =404;
}
location /api/ {
proxy_set_header Host my-backend-loadbalancer.com;
proxy_pass http://my-backend-loadbalancer.com$is_args$args;
}
}
I get a Bad Request error with this approach. I would like to know if I am following good practices in terms of accessing a backend within a private network and how can I make this work.
UPDATE
I have tried this configuration, but it seems that is not passing the arguments. Because when I try http://my-domain.com/api/ it redirects to http://my-backend-loadbalancer.com whit out any argument on it.
server {
listen 80;
listen [::]:80;
root /var/www/my-domain.com/html;
index index.html index.htm index.nginx-debian.html;
server_name www.my-domain.com my-domain.com;
location / {
try_files $uri $uri/ =404;
}
location /api/ {
proxy_pass http://my-backend-loadbalancer.com;
}
}
Here's what I found in error.log.
2022/11/04 15:57:52 [error] 4749#4749: *3 no resolver defined to resolve internal-backend-213123123.us-west-1.elb.amazonaws.com, client: 10.0.0.78, server: www.my-domain.com, request: "GET /api/ HTTP/1.1", host: "my-domain.com", referrer: "http://my-domain.com/"
Related
I created a SPA (React) with a Asp.Net Backend (Net6.0). I can access the Site but every backend call just returns a HTTP 500
Supervisor.conf:
[program:Service]
command=/home/pi/.dotnet/dotnet /var/service/API.dll
directory=/var/service/
autostart=true
autorestart=true
startsecs=0
stderr_logfile=/var/log/service.err.log
stdout_logfile=/var/log/service.out.log
environment=ASPNETCORE_ENVIRONMENT=Production,NUGET_PACKAGES="/home/anil/.nuget/packages"
user=pi
stopsignal=INT
nginx:
server {
listen 5001;
listen [::]:5001;
server_name localhost;
root /var/www/wwwroot;
index index.html;
location / {
try_files $uri /index.html;
error_page 405 #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 would like to have following Nginx urls by configuring Nginx as reverse proxy for both Jenkins and Nexus.
http://10.20.30.40 -> should display Nginx home page
http://10.20.30.40/jenkins -> should display Jenkins home page
http://10.20.30.40/nexus -> should display Nexus home page
After googling around I modified default configuration file and tried to check configuration. But I am getting following error while checking my configuration.
Note: I don't have any domain and ssl. Just using IP address for now.
sudo nginx -c /etc/nginx/nginx.conf -t
nginx: [emerg] invalid number of arguments in "proxy_pass" directive in /etc/nginx/sites-enabled/default:92
nginx: configuration file /etc/nginx/nginx.conf test failed
/etc/nginx/sites-available/default
# Default server configuration
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html;
index index.html index.htm index.nginx-debian.html;
server_name _;
location / {
try_files $uri $uri/ =404;
}
}
# Jenkins server configuration
server {
listen 80;
listen [::]:80;
server_name _;
root /var/www/html;
index index.html;
location /jenkins {
proxy_pass http://localhost:8080
try_files $uri $uri/ =404;
}
}
# Nexus server configuration
server {
listen 80;
listen [::]:80;
server_name _;
root /var/www/html;
index index.html;
location /nexus {
proxy_pass http://localhost:8081
try_files $uri $uri/ =404;
}
}
Create two upstream block outside of server block, both for jenkins and nexus like this:
upstream backendjenkins {
server <hostname>:8080;
}
upstream backendnexus {
server <hostname>:8081;
}
Then, in the server block, mention like this:
location /jenkins {
proxy_pass http://backendjenkins;
try_files $uri $uri/ =404;
}
location /nexus {
proxy_pass http://backendnexus;
try_files $uri $uri/ =404;
}
Hope, this might help you.
I have Nginx setup Flask based backend running on port 8080 with the below configuration
server {
listen 8080 default_server;
listen [::]:8080;
root /var/www/html;
server_name _;
location /static {
alias /var/www/html/static/;
}
location / {
try_files $uri #wsgi;
}
location #wsgi {
proxy_pass http://unix:/tmp/gunicorn.sock;
include proxy_params;
}
location ~* .(ogg|ogv|svg|svgz|eot|otf|woff|mp4|ttf|css|rss|atom|js|jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|tar|mid|midi|wav|bmp|rtf)$ {
access_log off;
log_not_found off;
expires max;
}
}
I have also setup a systemd service that uses gunicorn to run the flask app using: gunicorn --bind=unix:/tmp/gunicorn.sock --workers=4 start_backend:web_app
Now the above is working for Python Flask backend on port 8080, I also want to add Vue app on default port 80 as well.
Update
server {
listen 80 default_server;
listen [::]:80;
root /var/www/html/dist;
server_name _;
location /static {
alias /var/www/html/dist/static/;
}
location / {
root /var/www/html/dist;
try_files $uri $uri/ /index.html;
}
location /api {
root /var/www/html;
try_files $uri #wsgi;
}
location #wsgi {
proxy_pass http://unix:/tmp/gunicorn.sock;
include proxy_params;
}
Sounds like you need to add another server block to serve the frontend.
server {
listen 80 default_server;
listen [::]:80;
location / {
root /path/to/dist;
try_files $uri $uri/ /index.html;
}
}
I've based this code on this tutorial where /path/to/dist in the above example should be changed to the dist directory of the Vue.js front-end from your vue app.
If you have a read of the section Setting Up Nginx in this tutorial, you'll notice that they are serving the Flask application and the Vue.js fronted at different URLs in the same server block:
server {
listen 80;
server_name 123.45.67.89;
location /api {
include uwsgi_params;
uwsgi_pass unix:/home/survey/flask-vuejs-survey/backend/surveyapi.sock;
}
location / {
root /home/survey/flask-vuejs-survey/frontend/survey-spa/dist;
try_files $uri $uri/ /index.html;
}
If this app will be facing the internet then this is probably a better way to do things, as port 8080 outgoing may be blocked by your users' internet provider. With the second configuration everything is served through port 80.
You may have to adjust your vue.js app slightly to make it look for the API at /api (or something) leaving / free to serve the frontend.
I have one next.js server that is running on port 3000 and I have static build (created with create-react-app), that should be admin panel. So it looks like this
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
root /usr/share/nginx/city-am-club/admin/build/;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location /admin/ {
root /usr/share/nginx/myproject/admin/build;
index index.html index.htm;
try_files $uri /index.html;
default_type "text/html";
}
location / {
rewrite /(.*) /$1 break;
proxy_pass http://127.0.0.1:3000;
}
}
I understand that location should be like this with admni panel, cause location is path after root path.
location / {
root /usr/share/nginx/myproject/admin/build;
index index.html index.htm;
try_files $uri /index.html;
default_type "text/html";
}
Any way, I don't really know how to configure this correct. Right now I cannot get my built files, i tried a lot of different variations of this config. ATM I have a behavior when all my routes location /, even when I try to react /admin it shows me 404 page (custom page of locations / server template).
Try this for your NGINX config.
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
root /usr/share/nginx/city-am-club/admin/build/;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
rewrite /(.*) /$1 break;
proxy_pass http://127.0.0.1:3000;
location /admin/ {
alias /usr/share/nginx/myproject/admin/build;
index index.html index.htm;
try_files $uri /index.html;
default_type "text/html";
}
}
If the admin path is not /usr/share/nginx/myproject/admin/build then change the alias section.