how to use Nginx proxy_pass to a Flask APP - nginx

I one websites, ie. example.com. Its nginx config file is like,
server{
...
location /foo{
proxy_pass http://ip_address/;
proxy_set_header Host ip_address;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
...
}
For the above ip_address, I had following nginx config file of flask app,
server {
listen 80;
server_name ip_address;
location = / {
include uwsgi_params;
uwsgi_pass unix:/pathtomysocket/x.sock;
}
location / {
include uwsgi_params;
uwsgi_pass unix:/pathtomysocket/x.sock;
}
}
What I would like to achieve here is that, when client visits example.com/foo, it should response from the flask app, however, there is one issue that, the urls generated from the flask app don't have foo as prefix, so when click other link like /abc, it will redirect to example.com/abc rather than example.com/foo/abc. How can I acheive this result? I searched a lot but no luck, I guess it maybe relate to server name etc.. please help. thanks!

Perhaps you need to look into your routing configuration at your FlaskApp.
Look into Blueprints
This is another example of blueprint usage to prefix routes

Related

nginx reverse proxy - only allow access with "secret token"

I have set up a reverse proxy with the following code in nginx:
server {
listen 80;
server_name domain.com;
server_name www.domain.com;
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_pass http://ip-of-server:80;
}
}
Is it possible to let users only access my main server via domain.com/?some-secret-token and not let them access it if they are going to domain.com directly? In best case, the secret-token would also disappear from URL after they open it. I know it would be possible within my homepage script - but can I also configure my nginx in such a way without changing script files?
Don't think this is possible with pure nginx. You can, however, set up basic authentication on your url with something like this after you've properly configured a .htpasswd file:
location / {
auth_basic "Private";
auth_basic_user_file /etc/nginx/.htpasswd;
}
Full details of how to set this up are available here.

nginx Reverse Proxy with plesk

I've already seen some answers on here and none of the solutions seem to work.
I have domain.com with a wordpress install
and a script running on domain.com:6000
I want to be able to have script.domain.com show what's on domain.com:6000
Now the other big issue is plesk. (It gets a lot of hate but the people using the website like the UI.) but here's what I've done/tried
New folder and file in /var/www/vhosts/domain.com/conf
file : vhost_nginx.conf
and what's currently in it
server {
listen 80;
server_name script.domain.com;
location / {
proxy_pass http://domain.com:6000;
}
}
Also having tried
location /script/ {
proxy_pass http://domain.com:6000/;
}
to try and have domain.com/script show something different.
Any suggestions?
Right now in PLesk 12.5 there is no way to override "location /" via plesk, because all custom conf files are added at the end of nginx's server section after default "location /" derectives.
You can create or change hosting type of your subscription to forwarding like in this answer https://serverfault.com/a/541055/154664
But in this case port will be visible in URL.
Another solution is to create your own custom virtual host in nginx in some separate config - it's actually easiest way now.
Another solution is to customize virtual hosting templates, but it's too much side effects on Plesk upgrade.
I put this in the Plesk UI under additional nginx directives and it worked for me. You can remove the if, if you are ok with http traffic. Also replace <*> accordingly. For instance:
<your-domain> = script.domain.com, <your-host> = localhost <your-port> = 6000
if ($scheme = http) {
return 301 https://<your-domain>;
}
location ~ / {
proxy_pass http://<your-host>:<your-port>;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Host $server_name;
}

nginx reverse proxying multiple application requesting similar assets

I have several applications located at:
http://www.foo.com:80
http://www.bar.com:42
http://www.baz.com:1337
that I am attempting to reverse proxy with one nginx machine. the issue I have right now is that these applications are requesting files that are identical in name, but not identical in content:
location /bootstrap.css {
proxy_pass http://www.foo.com:80/bootstrap.css;
}
location /bootstrap.css {
proxy_pass http://www.bar.com:42/bootstrap.css;
}
location /baz {
proxy_pass http://www.baz.com:1337;
}
location /foo {
proxy_pass http://www.foo.com:80/;
}
is it possible for me to re-write all responses coming from a particular application server to point to it's application subfolder?
ex: redirect
http://www.foo.com:80/*
to
/foo
You can setup something like:
location /foo {
proxy_pass http://www.foo.com:80;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
However, your application foo might need to know about this. See this post for example.
EDIT:
Nginx might be able to do what you want by using sub_filter from ngx_http_sub_module
Try the example in: answer 1 and answer 2

How to configure nginx rules so that if one failed it serve the request using another

Note, this question is moved to stackoverflow from superuser
I got the following nginx conf:
server {
listen 80;
listen 443 ssl;
...
# specific rule to serve versioned js resources
location ~ ^/js/([0-9\.]+)/(.*)$ {
alias /opt/x/public/deploy/js/$1/$2;
}
location / {
add_header P3P 'CP="CAO PSA OUR"';
proxy_pass http://127.0.0.1:8088;
set $ssl off;
if ($scheme = https) {
set $ssl on;
}
proxy_set_header X-Forwarded-Ssl $ssl;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Real-IP $remote_addr;
}
}
It works as expected. However if a certain versioned js resource does not exists in the deployed dir, say /opt/x/public/deployed/js/1.1/, it will return 404. What I want is in that case nginx shall pass the request to the backend service running at 8088 port instead of returning 404. Is this even doable?
Thanks!
Green
http://wiki.nginx.org/HttpCoreModule#try_files
try_files is your friend, here you can do the order you want to try files and finaly have the proxypass upstream.

Change Host header in nginx reverse proxy

I am running nginx as reverse proxy for the site example.com to loadbalance a ruby application running in backend server. I have the following proxy_set_header field in nginx which will pass host headers to backend ruby. This is required by ruby app to identify the subdomain names.
location / {
proxy_pass http://rubyapp.com;
proxy_set_header Host $http_host;
}
Now I want to create an alias beta.example.com, but the host header passed to backend should still be www.example.com otherwise the ruby application will reject the requests. So I want something similar to below inside location directive.
if ($http_host = "beta.example.com") {
proxy_pass http://rubyapp.com;
proxy_set_header Host www.example.com;
}
What is the best way to do this?
You cannot use proxy_pass in if block, so I suggest to do something like this before setting proxy header:
set $my_host $http_host;
if ($http_host = "beta.example.com") {
set $my_host "www.example.com";
}
And now you can just use proxy_pass and proxy_set_header without if block:
location / {
proxy_pass http://rubyapp.com;
proxy_set_header Host $my_host;
}
map is better than set + if.
map $http_host $served_host {
default $http_host;
beta.example.com www.example.com;
}
server {
[...]
location / {
proxy_pass http://rubyapp.com;
proxy_set_header Host $served_host;
}
}
I was trying to solve the same situation, but with uwsgi_pass.
After some research, I figured out that, in this scenario, it's required to:
uwsgi_param HTTP_HOST $my_host;
Hope it helps someone else.
Just a small tip. Sometimes you may need to use X-Forwarded-Host instead of Host header. That was my case where Host header worked but only for standard HTTP port 80. If the app was exposed on non-standard port, then this port was lost when the app generated redirects. So finally what worked for me was:
proxy_set_header X-Forwarded-Host $http_host;

Resources