I tried to follow the instructions here, but I still can't seem to get it to work. As with the question, I don't expect some of the servers to be running at the time of starting nginx. I would actually prefer if instead a local 404 html were returned if the server was not running.
upstream main {
server my_main:8080;
}
server {
listen 80;
server_name www.my_site.com my_site.com;
location / {
resolver 8.8.8.8 valid=30s;
set $upstream_main main;
proxy_pass http://$upstream_main;
}
}
Related
I have a front-end react app, that runs at localhost:3000. And a back-end that runs at localhost:9900.
I can access my back-end with a postman's(or curl) localhost:80/api request.
But I can't access either the site or the server via the web address: my-1stconnection.lan.test.
What's the problem?
NGINX config:
server {
listen 80;
listen [::]:80;
server_name my-1stconnection.lan.test;
location / {
proxy_pass http://localhost:3000/;
}
location /api {
proxy_pass http://localhost:9900/;
}
}
I forgot one important thing in order to activate the local server:
127.0.0.1 my-1stconnection.lan.test into /etc/hosts
After this fix, everything started working.
We have a need to set up multiple up-stream server, and use proxy_next_upstream to a backup, if the main server returns 404. However, the URI for up-stream backup server is different than the one towards main server, so I don't know whether this can be possible.
In detail, below config snippet works fine (if URIs are the same to all up-stream servers):
upstream upstream-proj-a {
server server1.test.com;
server server2.test.com backup;
}
server {
listen 80;
listen [::]:80;
server_name www.test.com;
location /proj/proj-a {
proxy_next_upstream error timeout http_404;
proxy_pass http://upstream-proj-a/lib/proj/proj-a;
}
For a request of http://test.com/proj/proj-a/file, it will first try to request http://server1.test.com/lib/proj/proj-a/file, if return 404 or timeout, then try http://server2.test.com/lib/proj/proj-a/file. This is good.
However, now for server2, it can only accept URL like http://server2.test.com/lib/proj/proj-a-internal/file, which is different than the URI towards the main server. If only considering the backup server, I can write like below:
proxy_pass http://server2.test.com/lib/proj/proj-a-internal
However looks like I can not have different proxy_pass for different upstream server combining proxy_next_upstream.
How can I achieve this?
I found a work-around using simple proxy_pass, and set local host as the backup upstream server, then do rewrite on behalf of the real backup upstream server.
The config is like below:
upstream upstream-proj-a {
server server1.test.com:9991;
# Use localhost as backup
server localhost backup;
}
server {
listen 80;
listen [::]:80;
resolver 127.0.1.1;
server_name www.test.com;
location /lib/proj/proj-a {
# Do rewrite then proxy_pass to real upstream server
rewrite /lib/proj/proj-a/(.*) /lib/proj/proj-a-internal/$1 break;
proxy_pass http://server2.test.com:9992;
}
location /proj/proj-a {
proxy_next_upstream error timeout http_404;
proxy_pass http://upstream-proj-a/lib/proj/proj-a;
}
}
It works fine, but the only side-effect is that, when a request needs to go to the backup server, it creates another new HTTP request from localhost to localhost which seems to double the load to nginx. The goal is to transfer quite big files, and I am not sure if this impacts performance or not, especially if all the protocols are https instead of http.
I have a requirement to run the services (Zeppelin, grafana etc) using https. I have installed certificates and added below configuration to NGINX.
But still it is not working, please let me know if any other configuration to be added. Thanks!
server {
listen 443 ssl;
ssl on;
ssl_certificate ../crt/*****.crt;
ssl_certificate_key ../crt/*****.key;
.
.
.
location /zeppelin {
proxy_pass http://127.0.0.1:8080/#/;
}
}
Configure the zeppelin IP in upstream and use the upstream name in proxy_pass like below
upstream zeppelin {
server 127.0.0.1:8080;
}
server {
....
location / {
proxy_pass http://zeppelin;
}
....
}
To host zeppelin under 'zeppelin' context you have to change the zeppelin.server.context.path property in conf/zeppelin-site.xml
Configuring zeppelin using nginx needs more configuration like websocket proxying. You can refer the sample nginx configuration in this link
I am working with docker containers with reverse proxy for jenkins container and got into this issue.
My nginx custom config is as follow:
upstream jenkins {
server 172.17.0.2:8080;
}
server {
listen 80;
server_name jenkins;
location /jenkins {
proxy_pass http://172.17.0.2:8080;
}
Also, /etc/nginx/nginx.conf doesn't have any default root directory but still when I tried to access http://localhost/jenkins, it is giving me 404 with Problem accessing /jenkins. Reason:Not Found
I checked nginx error logs and it has "/etc/nginx/html/index.html" is not found
Though I have not set any /etc/nginx/html/ config, why it is giving me 404 error?
Can someone clarify my doubt?
ScreenShot
Something like this seems more approriate for the nginx part. If you declare an upstream, use it :
upstream jenkins {
server 172.17.0.2:8080;
}
server {
listen 80;
server_name jenkins;
location /jenkins {
proxy_pass http://jenkins;
}
}
For the docker part, I recommand using port mapping if you can. Because IP of docker containers change, you will have to edit you config file each time you recreate the jenkins container. With something like docker container run -d -p 127.0.0.1:8080:8080 my-jenkins-container-image you can modify your nginx config to something like :
upstream jenkins {
server 127.0.0.1:8080;
}
server {
listen 80;
server_name jenkins;
location /jenkins {
proxy_pass http://jenkins;
}
}
I wonder if there is a simple way (simple = nginx conf without any lua/perl extension) to achieve the following.
Given the following upstream server and listeners:
upstream backend{
server 1.2.3.4:9080;
server 1.2.3.4:9081;
server 1.2.3.4:9082;
}
server {
listen 8080;
listen 8081;
listen 8082;
...
proxy_pass backend;
}
The requirement is that all traffic that is connected to a given port, will be passed via proxy_pass to the equivalent port at upstream.
Perhaps the upstream would not be used in this case, rather I shall use $http_port or similar, any advise will be appreciated.
TL;DR: ngx_http_upstream_module isn't designed this way.
While it's possible by using "route method" for session affinity, such approach will bring you to very unclear config.
Consider using multiple server blocks. Something like:
server {
listen 8080;
location / {
proxy_pass http://1.2.3.4:9080;
}
}
server {
listen 8081;
location / {
proxy_pass http://1.2.3.4:9081;
}
}
# ...and so on
The solution I have ended up with was using Lua to set a variable (upstream_port) which then used as:
proxy_pass http://$upstream_host:$upstream_port$request_uri