reverse proxy mulitple ipython notebook servers - nginx

Currently we are running an Ipython notebook server behind a nginx proxy. This works well as it is a straightforward 1-to-1 mapping.
Now we want to run multipe notebook servers behind 1 proxy. Since these servers will be dynamically added, the proxying should be dynamic as well.
Ideally I'd like to proxy on a url subpath:
http://open.net/py1 -> http://secure1:8888
http://open.net/py2 -> http://secure2:8888
http://open.net/py3 -> http://secure3:8888
etc.
Problem with this approach is that Ipython doesn't use relative url's inside it's html. extract:
<script src="/static/.../promise.min.js"</script>
<script src="/static/.../require.js"</script>
<script> ...
So inside http://open.net/py2 require.js will be loaded via http://open.net/static/.../require.js which of course will result in a 502. It should be http://open.net/py2/static/.../require.js
Question: what's a good strategy to solve this?
Constraints:
I cannot touch the source html
I cannot use subdomains for each Ipython server (as they are dynamically added)

what's a good strategy to solve this?
Subdomains
I cannot use subdomains for each Ipython server (as they are dynamically added)
Not true.
# this will only py<some-digits> subdomain.
server {
listen 80;
server_name ~^(?<sub>py\d+)\.example\.com$;
# now you have $sub variable that contains subdomain
# and could be used to choose what server you want to connect
...
}
# catch all server block that simple shows 404 for any request
server {
listen 80 default_server;
return 404;
}

Related

Redirect default (80) port to 5000 - Flask + NGINX + Ubuntu

I'm successfully able to run a flask app on my IP:5000 path. A simple Hello World program that shows the output on my browser.
Now, what I would like to do is to configure NGINX with a proxy so that if I access only IP which apparently runs on a default port 80, it should navigate to port 5000 and show output of my application.
In other words...
This is working : IP:5000 -> Output = Hello world
This isn't working: IP -> This site can’t be reached
The server settings that I want to add would be something like this.
server {
listen 80;
server_name MY_IP;
location / {
proxy_pass http://127.0.0.1:5000;
}
}
However, I'm not sure where to add this? Should it be inside http block inside /etc/nginx/nginx.conf?
Updates: Based on the answers given below, I've managed to do the following.
I did restart nginx after this. However, I'm still facing the same issue. App works on IP:5000 but does not work on IP
The configuration you have mentioned should be in a separate file, assume example.com.conf under /etc/nginx/conf.d. You can put all the configuration in /etc/nginx/nginx.conf and it'll work, it's just that for readability we create separate configuration files which would be auto included when you add it inside conf.d.
Ok, the problem is fixed. As #senaps and #Mukanahallipatna had mentioned, I created the new configuration file under conf.d.
However, the most imp step that I was missing was this part mentioned in the below link.
It is recommended that you enable the most restrictive profile that will still allow the traffic you've configured. Since we haven't configured SSL for our server yet, in this guide, we will only need to allow traffic on port 80.
Reference Link
sudo ufw allow 'Nginx HTTP'
Now, everything is working fine.
Put the working blocks in a file with any_name.conf inside the folder named /etc/nginx/conf.d and it will be loaded automatically.
You will need to restart your nginx.
update:
What are you using to serve flask? if you are using uwsgi, then you should use configurations like this:
include uwsgi_params;
uwsgi_pass unix:path_to_your.sock;
Other options for uwsgi_pass are:
uwsgi_pass localhost:9000; #normal
uwsgi_pass uwsgi://localhost:9000;
uwsgi_pass suwsgi://[2001:db8::1]:9090; #uwsgi over ssl
If you are using gunicorn to serve your flask app, then your current configs should be fine, check if your app is running and if you can get your index page or not using 5000 port, then check for other problems. your configs looks good, maybe it's a problem on flask not being run?

How to manage many URLs / domains

I would like to get following situation:
I have domains: xxx.com zzz.com and yyy.com
I have one server: xxx.yyy.zz.qq
I would like to configure glassfish to start listening on port 80, and basing on the URL choose proper base catalog for my sites i.e.:
Scenario 1: Visitor is entering url xxx.com or www.xxx.com -> Glassfish receive request on port 80 and pick up catalog: ./glassfish4/myXXXcom/ where index.html for xxx.com is placed.
Scenario 2: Visitor is entering url zzz.com or www.zzz.com -> Glassfish receive request on port 80 and pick up catalog: ./glassfish4/anotherSite/ where index.html for zzz.com is placed.
What have I done:
Installed glassfish 4.1 on my server.
Changed A field of my domains to my server address.
Created virtual server:
glassfish4/bin/asadmin/create-virtual-server --hosts xxx.com xxx
Created http listener:
glassfish4/bin/asadmin create-http-listener --listeneraddress xxx.com --listenerport 80 --default-virtual-server xxx xxx
I think that I am doing something completely wrong here. How do I fix this problem?
If I understand correctly, what you need to do is, create two domains in glassfish or create a cluster and assign two instances of local glassfish instances. One running in port 28080 and another domain in 28081 and use nginx as the load balancer to forward the request to appropriate ports when requests comes from different domains. To make it clear, I am writing step by step
Create a new cluster in glassfish admin console
Create and assign a new local glassfish instance to cluster. This instance will be running in port 28080 and handles requests coming from example1.com
Create another glassfish domain 28081 as the port no for handling example2.com
Install nginx, this acts as proxy and forward request to appropriate
domains. Nginx will be running in port 80.
Start the cluster
Configure nginx as below. This is the crucial part
server {
listen 80;
server_name example1.com;
location / {
proxy_pass http://127.0.0.1:28080;
}
}
server {
listen 80;
server_name example2.com;
location / {
proxy_pass http://127.0.0.1:28081;
}
}
Start nginx
I hope you are familiar with creating clusters and domains in glassfish. If you are unfamiliar with creating clusters in commandline. Glassfish admin console is there, where you can achieve everything. If you need more info, please feel free to write in comments.

Using nginx to serve local files instead of remote files

Say I'm accessing www.mywebsite.com.
This website fetches the following asset:
http://www.mywebsite.com/styles/app.css
I want to access the website exactly as I normally would, with one exception:
Whenever my browser makes a request to /styles/app.css, instead of fetching it from http://www.mywebsite.com, I want to fetch it from http://localhost:3000/mywebsite/.
So instead it should be fetching:
http://localhost:3000/mywebsite/styles/app.css
Is this possible with nginx?
I tried to do it using the following server config:
{
...
server {
listen 80;
server_name mywebsite.com;
location /styles/ {
proxy_pass http://localhost:3000/mywebsite/styles/;
}
}
But even after restarting nginx (sudo nginx -s quit, sudo nginx), nothing seems to have changed.
When I browse to www.mywebsite.com/styles/app.css, I still get the same old app.css being retrieved from the server, rather than my local one.

is my nginx config correct?

hi im trying to run my Ruby on rails app in nginx using
passenger start -e production
but it is missing the cache: [HEAD /] miss
im guessing this i dont have actualy a file in public sorry for this question this may be to easy to answer and when i route to www.tock.com it renders a live page in the internet :(
server {
listen 80;
server_name www.tock.com;
passenger_enabled on;
root /home/led/Aptana\ Studio\ 3\ Workspace/djors/public;
}
Where ever you point the webserver, nginx in this case, you need your DNS to match the location. If this is your production server, then you need DNS records to point www.tock.com to your server.
If this is your development or local machine, you probably don't want to name your server something that will overwrite the public DNS records. For example, I name all of my apps in my local nginx config like the following:
server_name my_app_name.local
Once you've given it a name, you'll need to add "my_app_name.local" to your hosts file (your local DNS records). Your hosts file should now have entries like below.
127.0.0.1 localhost
127.0.0.1 my_app_name.local
Restart nginx, and you can now goto my_app_name.local in your browser.
You can get rid of passenger and nginx conf all together, as it looks like you are doing this locally and if you want named links (as opposed to just running bundle exec rails server; use Pow to facilitate this. Personally, i'm a rails server guy, but ymmv.

nginx on separate server proxy_pass to multiple rails apps with sub URI on passenger standalone in different boxes

I have this requirement, where there are multiple rails applications. Each application is deployed in two app servers, (app1 and app2) and they are load balanced through nginx on a separate server (lb).
The lb box contains plain vanilla nginx without passenger plugins.
The rails applications are deployed on passenger stand alone.
All the rails applications need to run on the same domain but with different sub_uri, like below
http://www.example.com/rails1
http://www.example.com/rails2
I have the lb box nginx configuration something like below.
http {
...
upstream rails1_cluster {
ip_hash;
server app1.server:3001;
server app2.server:3001;
}
upstream rails2_cluster {
ip_hash;
server app1.server:3002;
server app2.server:3002;
}
...
server {
server_name www.example.com;
...
...
location /rails1 {
proxy_pass http://rails1_cluster;
...
}
location /rails2 {
proxy_pass http://rails2_cluster;
...
}
....
}
}
With this setup, the app running on passenger standalone in app1 and app2 throws an error that it is unable to find any route /rails1/.
This article "How To Deploy Phusion Passenger To A Subdirectory, Routing Errors, And Restarting" tries to address the same problem, but it suggests changing the routes, which I don't wish to do. The Rails applications am dealing with are of same code base but customized for specific instances catering to specific client.
In passenger plugin for Nginx server, there is a passenger_base_uri which helps in setting a sub URI for the app. What is the equivalent of the same in case of passenger stand alone? Or am I missing something fundamental here? Any help, suggestions would help.
Give this a try, using the rewrite module:
location /rails2 {
rewrite "/rails2/" / break;
proxy_pass http://rails2_cluster;
}
It's a regex so might go on fire if the url actually contains that. Also this one does not yet work for addresses without the trailing slash, so check this.

Resources