Hi I am facing issue when configuring nginx as proxy server to redirect request to my tomcat server. I have 3 tomcat server running on different machine & different port like this
192.168.51.115:8115
192.168.51.120:8120
192.168.51.130:8130
Now I want to config nginx to pass request to my three server sequentially like this
www.example.com/app1
www.example.com/app2
www.example.com/app3
Real IP: 123.123.123.123
This is my configuration under - site-enabled
server {
listen 80;
server_name example.com www.example.com;
location /app1 {
proxy_pass "http://192.168.51.115:8115";
}
location /app2 {
proxy_pass "http://192.168.51.120:8120";
}
location /app3 {
proxy_pass http://192.168.51.130:8130;
}
}
Note: When i put location directive placing just / then it works but doesn't work on /* like app1,app2 or app3
Can you try using ^~ as modifier in your location block ?
like
server {
listen 80;
server_name example.com www.example.com;
location ^~ /app1 {
proxy_pass "http://192.168.51.115:8115";
}
location ^~ /app2 {
proxy_pass "http://192.168.51.120:8120";
}
location ^~ /app3 {
proxy_pass http://192.168.51.130:8130;
}
}
Related
I have setup a docker environment for testing nginx reverse proxy.
Instead of setting up a subdomain for every backend, i would like to have 1 subdomain with a location pointing to the individual backend:
while this works as expected:
server {
listen 80;
server_name dashboard.test.lan;
location / {
proxy_pass http://172.17.0.1:82;
}
}
server {
listen 80;
server_name gitlab.test.lan;
location / {
proxy_pass http://172.17.0.1:83;
}
}
server {
listen 80;
server_name portainer.test.lan;
location / {
proxy_pass https://172.17.0.1:9443;
}
}
...this does not work:
server {
listen 80;
server_name proxy.test.lan;
location /dashboard {
proxy_pass http://172.17.0.1:82;
}
location /gitlab {
proxy_pass http://172.17.0.1:83;
}
location /portainer {
proxy_pass https://172.17.0.1:9443;
}
}
Maybe you guys can give me a hint or explanation why this does not work.
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.
My Nginx installed and running, below is the config from /etc/nginx/nginx.conf , I want to forward all /api/* to my tomcat server, which is running on the same server at port 9100(type http://myhost:9100/api/apps works) , otherwise, serve static file under '/usr/share/nginx/html'. Now I type http://myhost/api/apps give an 404. What's the problem here?
upstream myserver {
server localhost:9100 weight=1;
}
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
root /usr/share/nginx/html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location ^~ /api/ {
proxy_pass http://myserver/;
}
location / {
}
}
The proxy_pass statement may optionally modify the URI before passing it upstream. See this document for details.
In this form:
location ^~ /api/ {
proxy_pass http://myserver/;
}
The URI /api/foo is passed to http://myserver/foo.
By deleting the trailing / from the proxy_pass statement:
location ^~ /api/ {
proxy_pass http://myserver;
}
The URI /api/foo is now passed to http://myserver/api/foo.
I want to have two Play Framework deployments on the same server and configure nginx to redirect the locations accordingly.
I have this configuration:
server {
listen 80;
server_name localhost;
client_max_body_size 20M;
location /site2 {
proxy_pass http://localhost:8000/;
}
location / {
proxy_pass http://localhost:9000/;
}
}
But it does not work.
What I want is:
When I use: http://ip/ or http://ip/something the Play deployment at port 9000 should respond.
When I use: http://ip/site2/ or http://ip/site2/something the Play deployment at port 8000 should respond.
First, please check that you have access to each app directly:
ip:8000
ip:9000
Next please add proxy_redirect directive:
server {
listen 80;
server_name localhost;
location /site2 {
proxy_pass http://localhost:8000/;
proxy_redirect http://localhost/site2/ http://localhost:8000/;
}
location / {
proxy_pass http://localhost:9000/;
proxy_redirect http://localhost/ http://localhost:9000/;
}
}
I want nginx to search my local host for the file first and on a 404 error it should search server 1.1.1.1.
I am able to fetch the file that is located on local host, but not able to get from server 1.1.1.1.
server {
listen 80;
server_name localhost;
access_log /var/log/nginx/access.log main;
location /products/ {
proxy_next_upstream http_404;
root /var/foo;
}
}
server {
listen 80;
server_name 1.1.1.1;
location /products/ {
#########
}
}
I guess proxy_next_upstream is not switching to the server.
Any help on this would be appreciated.
The proxy_next_upstream directive is a configuration directive to control re-request from a group of upstream servers by a proxy_pass if request to one of them fails. It doesn't make sense without proxy_pass and an upstream block defined. You may use it if you proxy to multiple upstream servers like this:
upstream backends {
server 192.2.0.1;
server 192.2.0.2;
...
}
server {
...
location / {
proxy_pass http://backends;
proxy_next_upstream error timeout http_404;
}
}
If you want nginx to search for a file on disk, and if it's not found - proxy request to another server, configure it e.g. using try_files fallback instead:
location / {
root /path/to/root;
try_files $uri #fallback;
}
location #fallback {
proxy_pass http://...
}
See http://nginx.org/r/try_files for more info about the try_files directive.