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;
}
}
Related
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/"
I have implemented a flask rest server with swagger-ui using flask-restx.
I could get the swagger-ui working when running the server using command, without nginx
flask run --host=0.0.0.0
or
uwsgi --ini app.ini
My app.ini:
[uwsgi]
module = wsgi:app
master = true
processes = 2
socket = /tmp/myproj.sock
chmod-socket = 666
vacuum = true
die-on-term = true
====================
However, with nginx, although my REST APIs are working, I couldn't get the swagger-UI.
Error message I received on browser:
My nginx configuration in /etc/nginx/sites-available/default:
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
location /api {
include uwsgi_params;
uwsgi_pass unix:/tmp/myproj.sock;
}
}
Any idea how to configure nginx so that swagger-UI could be loaded?
Thank you.
Update
I managed to make it works by adding /swaggerui in nginx configuration.
Example code:
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/ /index.html;
}
location /api {
include uwsgi_params;
uwsgi_pass unix:/tmp/myproj.sock;
}
location /swaggerui {
include uwsgi_params;
uwsgi_pass unix:/tmp/myproj.sock;
}
}
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 config a static site via nginx, and include my settings under conf.d directory. listen port 80.
But I found when I request the url, nginx always redirect the default page /usr/share/nginx/html/index.html
My configuration does seem work, for all access logs were written to my access log settings, and if I change index.html to some other name(i.html for example) in my directory, and request url mysite.com/i.html, I can access the correct page.
So, it seems that nginx redirect all index.html to the default one.
I tried change default port/server name/root and annotate the default settings, even close selinux, all above doesn't work, it really make me mad.
Can anyone help me?
I'm using nginx version 1.10.2, on CentOS 7.2
and following is my site settings:
# the upstream component nginx needs to connect to
upstream blog {
server 0.0.0.0:80; # for a web port socket (we'll use this first)
}
# configuration of the server
server {
# the port your site will be served on
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
# the domain name it will serve for
server_name mysite.com;
charset utf-8;
access_log /var/log/blog/access.log;
error_log /var/log/blog/error.log;
# max upload size
client_max_body_size 75M; # adjust to taste
# Finally, send all non-media requests to the Django server.
location / { try_files $uri #blog; }
location #blog{
root /home/work/projects/blog/public/;
index index.html index.htm;
uwsgi_read_timeout 120s;
uwsgi_send_timeout 120s;
}
}
and the /etc/nginx/nginx.conf is
server {
# listen 8000 default_server;
# listen [::]:8000 default_server;
# server_name not;
# root /usr/share/nginx/html;
# Load configuration files for the default server block.
# include /etc/nginx/default.d/*.conf;
# location / {
# }
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
This makes no sense:
location / {
try_files $uri #blog;
}
location #blog{
root /home/work/projects/blog/public/;
index index.html index.htm;
uwsgi_read_timeout 120s;
uwsgi_send_timeout 120s;
}
The lack of a root directive means that the first try_files will look for files in the default location.
All you need is root and index directives within the server context:
For example:
root /home/work/projects/blog/public;
index index.html index.htm;
location / {
try_files $uri $uri/ =404;
}
See this document for details.