opening other websites links in nginx server - nginx

I am new to Nginx and I have set up the server using this tutorial. Now when I add links to other sites like http://www.anotherwebsite.com and when I click on this link from my page the server directs me to http://www.mywebsite.com/http://www.anotherwebsite.com. The server is appending the other link to my website link. How do I change that. I have looked into these places
How to redirect a url in NGINX,
Rewrite to pretty links with nginx
but I just cant make it work. Any help will be appreciated. Thanks
server {
listen 80;
server_name $hostname;
location /static {
alias /var/www/<app-name>/static;
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
location / {
include uwsgi_params;
uwsgi_pass unix:/tmp/uwsgi.sock;
uwsgi_param UWSGI_PYHOME /var/www/<app-name>/env;
uwsgi_param UWSGI_CHDIR /var/www/<app-name>/project;
uwsgi_param UWSGI_MODULE <project-name>.wsgi:application;
}
}

you're not doing any redirecting at all from your posted nginx config,
everything except /static/ and /50x.html is passed on to the uwsgi app
consequently the redirecting has to be happening in the uwsgi app
As far as redirecting from within nginx goes, the simple case goes like this:
location /redirected-path {
#for 302 redirect
rewrite ^ http://anotherwebsite.example.com/path-on-other-site redirect;
#for 301 redirect
# rewrite ^ http://anotherwebsite.example.com/path-on-other-site permanent;
}
(the more complicated cases involve regexes more complicated then ^)
UPDATE:
right, so from the code you linked in the comment below, what you really want to do is change the href value of the outputted html code anchor tag.
The proper place to do so is in the backend code (i.e. in the uwsgi app you're connecting to)
You could do it with the following rewrite:
location /main {
rewrite ^/main(.*) http://www.mysite.com$1 permanent;
}
BUT this has the big drawback of requiring an extra roundtrip to the server, a client then does:
request to your server
response with redirect to the other server
request to the other server
response from the other server
whereas if you change it in the backend code steps 1 and 2 are no longer needed.
Besides causing a potentially (depending on connection speed and server load) noticable delay. It will also increases your server load.
Doing it with a server-rewrite is a hack you really should skip unless you have no access to the backend code

Related

path based routing as a reverse proxy

We were looking to use the AWS application load balancer (ALB) to direct incoming requests to specific servers where a "tenant" may reside. For example - customerA signs up to our free trial and we provision them a new instance of our application on webserver1 and we send them a link to their app, eg. https://trial.our.app/customerA
What we need is for our automated deployment that we will design and develop to ensure that when that person hits the load balancer it directs them to the appropriate server. However, from what we understand the AWS ALB has a rule limit of 100 rules therefore if we hit 100 free trials we would have run out of rules :
if path is '/customerA/*' then forward to webserver1
if path is '/customerB/*' then forward to webserver1
...
if path is '/customerX/*' then forward to webserver2
if path is '/customerY/*' then forward to webserver3
etc.
Also, if the customer browses to the "root" https://trial.our.app it redirects them to our website free trial page (different URL, eg. https://ourwebsite.io/freetrial)
We're not looking to load balance, it will simply be a case of redirecting the customer to their appropriate webserver where they have been provisioned.
We're considering NGINX (and are new to it!) and have spun it up to test this out and it mostly works, however we are unsure of a few things:
what would a basic/sample configuration of this look like? what he have so far on our test is as follows:
server {
listen 80;
listen [::]:80;
server_name _;
root /usr/share/nginx/html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
error_page 404 /404.html;
location = /404.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
location = / {
return 301 https://ourwebsite.io/freetrial;
}
location /customerA {
proxy_pass http://10.101.1.149/;
}
location /customerB {
proxy_pass http://10.101.2.34/;
}
}
how do we ensure the location path is case insensitive, processes and includes everything after the path , eg. https://trial.our.app/customerA/#/app
how would you go about automating this so that the nginx config is updated when a new customer signs up and is added to the list of "locations" in the nginx.conf file?
any other suggestions of how best to approach this? are we using the right approach?

Nginx reverse proxy - Internal servers separated by trailing slash

I'm a newbie at Nginx, and have been searching a lot for the right answer to my question, but couldn't find it; not because it is not there, but my newbie condition limits me to adapt a generic solution to my issue.
The situation is this:
I have a Mantis Bug Tracker in my private LAN (http://10.111.111.12).
On the other hand, i have an OwnCloud website also on my LAN (IP 10.111.111.5), with URL http://10.111.111.5/owncloud/.
What i want to do is to deploy a Nginx Reverse Proxy that handles all requests from Internet at publicdomain.com, and use trailing slash for each internal webserver. The desired result would be:
http://www.publicdomain.com/bugtracker -> redirects to http://10.111.111.12/index.php
http://www.publicdomain.com/cloud -> redirects to http://10.111.111.5/owncloud/ (note that "cloud" is preferred over "owncloud")
On the future, it is necessary to continue using trailing slash for other web servers to be deployed.
Questions are:
is this scenario possible? if so, is it enough with configuring nginx or I have to reconfigure internal web servers as well?
I really appreciate your help, by indicating me a possible solution or pointing me to the right direction on previous posts.
Thanks a lot in advance.
Yes it is possible to achieve such configuration and it's commonly used when NGINX is acting as a reverse proxy. You can use this configuration as an inspiration for building your own:
upstream bugtracker {
server 10.111.111.12;
}
upstream cloudupstream {
server 10.111.111.5;
}
server {
listen 80;
location /bugtracker/{
proxy_pass http://bugtracker;
}
location /cloud/{
proxy_pass http://cloudupstream/owncloud;
}
}
What's happening over here is that nginx will be listening on port 80 and as soon as a request comes for path /bugtracker, it will automatically route the request to the upstream server mentioned above. Using this you can add as many upstream servers and location blocks as you want.
Reference: http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_pass
Thanks a lot Namam for your quick answer. However, it isn't working yet. It seems that "server" at upstream directive does not allow slash, like this:
server 10.111.111.5/owncloud;
If i used it, i obtained
nginx: [emerg] invalid host in upstream "10.111.111.5/owncloud" in /etc/nginx/nginx.conf:43
I started with the first upstream bugtracker, only, and nginx.conf like this:
upstream bugtracker {
server 10.111.111.12;
}
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name localhost;
root /usr/share/nginx/html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
}
location /mstic{
proxy_pass http://bugtracker;
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
After that, when accesing to my Nginx Reverse proxy http://10.111.111.10/mstic/ i obtain the following:
Not Found The requested URL /mstic/ was not found on this server.
and no further details on error or access logs.
Thanks a lot in advance for any extra help you could bring me.

NGINX - redirect to a custom maintenance page without changing url

I have a NGINX server running, now I have the following directive and location block for a custom 503 error page:
error_page 503 #maintenance;
location #maintenance {
rewrite ^(.*)$ /maintenance.html break;
}
When a user is redirected to the custom 503 error page, the url in his browser changes to the following:
https://www.example.com/maintenance
I would like the user to be redirected to the maintenance but keep the root domain in the address bar:
https://www.example.com/
How can I achieve that?
Thanks in adavnce!

Nginx 301 redirection options

I have some 100 or so URLs that need to be permanently redirected from static HTML to dynamic pages served by Wordpress. All examples for Nginx 301 redirection I found suggest that each redirect should be defined as its own server block.
However, I have found out that multiple redirects within one server block work as well such as in this simplified configuration:
server {
listen 80;
server_name www.example.com;
root /var/www/;
location = /subdir/red1.html { return 301 /subdir/?p=1; }
location = /subdir/red2.html { return 301 /subdir/?p=2; }
location = /subdir/red3.html { return 301 /subdir/?p=3; }
}
Curl -I confirms the 301 code. The redirects take place. I prefer this configuration to the one with one server block per redirect because human readability is better. But does this configuration lack something that I'd otherwise have if each redirect was in its own server block?
Well, execrpts you found are probably dealing with http to https redirections where it makes sense not to have anything more in the server block because the vhost is only there to redirect to an other domain. That's completely wrong to pick random examples and take them as a rules of thumb instead of refering to the official documentation.
100 redirects is not a huge count, you could even use permanent rewrites in one unique server block and group them with regexs.
server {
server_name www.example.com;
root /var/www/;
location /subdir/ {
rewrite ^/subdir/red([1-3])\.html /subdir/?p=$1 permanent;
}
}

Nginx 502 when serving error page content?

I've been setting up Nginx as a reverse proxy for an app on the server. Part of this includes a maintenance page that has external content (like images). I was able to find a method for setting up an error page with images returning 200, but it looks like a reverse proxy will change the whole environment. Here's the original solution from nginx maintenance page with content issue
error_page 503 #maintenance;
location #maintenance {
root /path_to_static_root;
if (!-f $request_filename) {
rewrite ^(.*)$ /rest_of_path/maintenance.html break;
}
return 200;
}
The Reverse Proxy is configured as:
location / {
proxy_pass http://127.0.0.1:9007/;
proxy_redirect off;
}
The Problem is that when a file is found to exist in the "maintenance" root, something goes wrong and the server returns a 502. Anyone know what the cause could be?
Some speculation I'm wondering if server listens on port 80, it somehow passes any good file request back into the proxy. If that were true, how would that be avoided?
Edit
Here's the error in the nginx log. I am directly trying to access 50x.html. Not sure why this would occur?
2012/02/17 19:39:15 [error] 21394#0: *13 connect() failed (111: Connection refused) while connecting to upstream, client: (my ip address), server: _, request: "GET /50x.html HTTP/1.1", upstream: "http://127.0.0.1:9007/50x.html", host: "domain.com"
It looks like it is indeed trying to GET from the app and not the root. How can I bypass this?
Edit 2
I originally thought I had found an answer where a change was made for nginx v1.0.12 but it did not solve the problem. It involved a similar situation but my guess is the fix was too specific.
You shouldn't need to involve the backend (I.E., shouldn't use proxy pass) since your maintenance page should be a static html file that Nginx can serve directly.
Assuming you have a setup configured as ...
server {
listen 80;
server_name example.com;
root /path/to/webroot;
# Regular locations etc
...
}
Create a folder called "503_status" and put your maintenance page in there as "503.html".
With that in place, create a file called "maintenance.default" under the Nginx directory with the following content ...
error_page 503 /503_status/503.html;
# Candidate for redirection if not ending with one of these extensions.
if ( $request_uri !~ \.(jpg|gif|png|css|js)$ ) {
set $maint "Tr";
}
# Candidate for redirection if not a request for the maintenance page
if ( $request_uri !~ ^/maintenance/$ ) {
set $maint "${maint}ue";
}
# Redirect to a location where the status code will be issued
if ( $maint = True ) {
rewrite ^ /maintenance/ redirect;
}
# Due to Nginx quirk, issue the status code in a location context.
# Make "internal" to prevent direct browsing.
location /maintenance {
internal;
return 503;
}
# 503_status folder as "internal" so no direct browsing
location 503_status {
internal;
alias /server/path/to/503_status/folder;
}
Whenever you put to put the site into maintenance, just include the file as follows ...
server {
listen 80;
server_name example.com;
root /path/to/webroot;
include /server/path/to/maintenance.default;
# Regular locations etc
...
}
This will serve your maintenance page as well as any resources it needs (just make sure extension is in the list). The backend server does not come into play at all.

Resources