I know that this is a common question, and there are answers for the same, but the reason I ask this question is because I do not know how to approach the solution. Depending on the way I decide to do it, the solution I can pick changes. Anyways,
I have an AWS EC2 instance. My DNS is handled by Route53 and I own example.com. Currently, on my instance, there are two services running:
example.com:80 [nginx/php/wordpress]
example.com:8142 [flask]
What I want to do is, make app.example.com point to example.com:8142. How exactly do I go about doing this? I am pretty sure that I will have to point app.example.com to the same IP as example.com, since it is the same box that will be serving it. And, nginx will be the first one to handle these requests at port 80. Is there a way with which I can make nginx forward all requests to localhost:8142?
Is there a better way that I can solve this problem?
You could add a virtual host for app.example.com that listens on port 80 then proxy pass all requests to flask:
server {
listen 80;
server_name app.example.com;
location / {
proxy_pass http://localhost:8142;
}
}
This is how you would do it with apache.
$cat /etc/apache2/sites-available/app.conf
<VirtualHost *:80>
ServerName app.example.com
ProxyPreserveHost On
<Proxy *>
Order allow,deny
Allow from all
</Proxy>
ProxyPass / http://localhost:8142/
ProxyPassReverse / http://localhost:8142/
</VirtualHost>
You can redirect your domain to a certain port. This depends on the web service you are using -Nginx/Apache. If you are using Nginx, you’ll need to do add a server block to your Nginx’s website config. This can be achieved by using the bellow
location /{
proxy_pass http://127.0.0.1:8142/;
}
If you are using Apache, you have two options, the first one is to add a redirection rule in your website’s .htaccess and the second one would be to do it directly in the Apache’s Vhost file. I like using the first option. In your .htaccess file, you can add the following rule
RewriteEngine on
# redirect to 3000 if current port is not 3000 and "some-prefix/" is matched
RewriteRule ^/(.*[^/])/?$ http://blabla:3000/$1/ [R=301,L]
If you want to use Apache’s Vhost file, I’ll recommend going through the following tutorial link
I have ubuntu 16 and nginx with two NodeJS instances, one for front, one for admin.
In, I have:
/etc/nginx/sites-available/default
I've added:
server {
...
location / {
proxy_pass http://127.0.0.1:8001;
}
location /admin {
rewrite ^/admin(.*) /$1 break;
proxy_pass http://127.0.0.1:8002;
}
location /other {
rewrite ^/other(.*) /$1 break;
proxy_pass http://127.0.0.1:8003;
}
...
}
I've used this to have access for admin.
Related
I'm moving some small websites in production to DDEV and, some of them has multiple domains with a 301 redirection to the main HTTPS site.
This config was working well with the "natural" Nginx when I was using a .conf file to manage the domains that should be redirect to the main site on this way:
server {
listen 80;
server_name .domain1.com
.domain2.com
.domain3.com
;
return 301 https://www.maindomain.com;
}
I tried to create a new domains.conf file and add it inside the .ddev/nginx_full directory to be loaded in the restart process but seems the Nginx didn't recognize such file.
In the main "natural" Nginx config file I has this server to redirect all requests coming from HTTP to HTTPS:
server {
listen 80;
access_log off;
error_log off;
server_name maindomain.com www.maindomain.com;
return 301 https://www.$host$request_uri;
}
I tried to add these configs inside the .ddev/nginx_full/nginx-site.conf file but the server start to be crazy, doing sometimes infinite redirections and sometimes, not recognize the domains.
Inside the config.yaml file I have:
additional_fqdns:
- domain1.com
- domain2.com
- domain3.com
- maindomain.com
- www.maindomain.com
use_dns_when_possible: false
I'm sure that's a "right way" to handle this situation but, looking the docs, I didn't find and answer for that. On this way, I ask if someone here have the catch for that.
Thanks a lot
I think this will work for you.
Add the file .ddev/nginx/redirect.conf with these contents:
if ($http_x_forwarded_proto = "http") {
return 301 https://$host$request_uri;
}
This uses a DDEV nginx snippet, it could also be done with a full nginx config.
The ddev-router acts as a reverse proxy that terminates SSL/443 and passes along requests on port 80 to the web container.
You see the infinite redirects because it sees the request always on port 80.
In DigitalOcean, we have all website.net pointing all traffic to website.com. Anyone accessing the .net will be redirected to .com
We have a scenario where we want the above to remain intact, but we want just one page to be loaded on the .net and not be redirected to the .com
website.net/reset should be accessible without any redirect happening to .com but we're not really sure how to do that, as it's an edge case we're solving where this is the only solution at the moment.
Any idea how to make website.net/reset not redirect to website.com using DigitalOcean?
UPDATE:
DNS for .com
DNS for .net
Nginx configuration for .net
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name www.website.net;
# Match /reset request exactly and redirect
location /reset {
# Redirect /reset to google does not work, redirects to website.com instead
# return 301 http://google.com/;
# Redirect /reset to website.net/reset does not work, redirects to website.com instead
return 301 http://www.website.net/reset;
}
# Redirect .net to .com
return 301 https://www.website.com$request_uri;
...
}
Added DNS for .com/.net and nginx configuration for .net, but still website.net/reset redirects to website.com. Any idea how to change this so it goes to website.net/reset and not redirect to website.com?
The easiest way to do that would probably be to update the server configuration for the .net server to issue a 301 redirect response to the .com address for everything except website.com/reset. The details of this would depend on which web server you are using, eg nginx, apache, etc.
For Apache this could be done using the RedirectMatch directive:
<VirtualHost digitaloceanwebsite.net:80>
ServerName digitaloceanwebsite.net
# Respond with a 301 redirect to all requests not made to /reset, /other, or /different/page
RedirectMatch permanent ^(?!\/reset$|\/other$|\/different\/page$) http://digitaloceanwebsite.com
DocumentRoot "/srv/http/digitaloceanwebsite/net"
<Directory "/srv/http/digitaloceanwebsite/net">
...
</Directory>
</VirtualHost>
<VirtualHost digitaloceanwebsite.com:80>
ServerName digitaloceanwebsite.com
DocumentRoot "/srv/http/digitaloceanwebsite/com"
<Directory "/srv/http/digitaloceanwebsite/com">
...
</Directory>
</VirtualHost>
If your websites are hosted on separate servers, just make sure that the website.net server has the RedirectMatch in its config.
Update response:
Looking at the nginx config that you posted, it looks like the 301 return in the server block is taking precedence over the location block.
This nginx config should work for what you want to do.
server {
...
server_name www.website.net;
# = specifies that this is an exact match which immediately takes precedence
location = /reset {
# location block for www.website.net/reset
}
# redirects all other requests
location / {
return 301 https://www.website.com$request_uri;
}
}
Also, if you need to add other exceptions or just want to do some further reading on this, this page at Digital Ocean has a good explanation of how nginx selects which location and server block to use.
I work on one website which is travel blog and there is sub section mytravelsite.com/tickets which is whitelabel and everything under that mytravelsite.com/tickets/fares and other pages are pointing to another ip and this works fine.
But now i would like to switch and now main site to be mytravelsite.com which would be whitelabel from mytravelsite.com/tickets but without /tickets in url but still keep my blog so another trouble is that whitelabel don't have robots.txt so this i would have to serve too from my hosting.
So in the end i have hosting with 10.1.1.1 ip and whitelabel that resolve to 10.10.10.10 ip
What i need is:
everything under mytravelsite.com/blog to resolve to my hosting and my hosted website on 10.1.1.1
and also mytravelsite.com/robots.txt and mytravelsite.com/sitemap.xml to resolve to my hosting on 10.1.1.1
and everything else to resolve to the whitelabel at 10.10.10.10
i am guessing that this can be done either with some dns setup or nginx proxy or rewrite rules but everything i searched on internet for last 2 days ended up in failure.
DNS only considers the name, i.e. the part before the slash. You have always mytravelsite.com, so this can't be done purely in DNS.
I know this isn't a real/full answer.
I would try to solve it using proxy.
The included snippet is not a configuration of nginx but of an apache as I don't have similar setup for nginx within my reach now. But I believe it can be useful starting point though.
<VirtualHost 10.10.10.10:80>
ServerName mytravelsite.com
ProxyPreserveHost On
ProxyPass /blog/ http://10.1.1.1/ retry=1 timeout=600 keepalive=On
ProxyPassReverse /blog/ http://10.1.1.1/
ProxyPass /robots.txt http://10.1.1.1/ retry=1 timeout=600 keepalive=On
ProxyPassReverse /robots.txt http://10.1.1.1/
ProxyPass /sitemap.xml http://10.1.1.1/ retry=1 timeout=600 keepalive=On
ProxyPassReverse /sitemap.xml http://10.1.1.1/
</VirtualHost>
Notes:
10.*.*.* is a local-only network, so the 10.10.10.10 within the
virtualhost header is just for an illustration on what server this
definition should be put.
It also will not be working out of the box - just start here and
iterate to a final solution
You have not defined how could "your hosted website" be identified, therefore it is missing in the example
Using nginx.conf features like proxy-pass/rewrite, can I keep the original URL in the browser's Location field?
I have several PlayFramework apps running on different ports (9001, 9002, ...) with proxy forwarding set up via nginx.conf. People browse to them as:
http://domain.name/App1/
http://domain.name/App2/
etc.
My nginx.conf entries look like this:
location /App1/ {
proxy_pass http://localhost:9001/;
rewrite ^/App1/(.*) http://domain.name:9001/$1;
}
If I ask for http://domain.name/App1/, what I see in the browser's Location field is http://domain.name:9001. What I wish I saw was http://domain.name/App1/, that is, I want the name App1 to remain in the URI, and I'd rather not expose the port number.
Let's say App1 has a link /location/ABC. When I click on it I see http://domain.name:9001/location/ABC when I wish I saw http://domain.name/App1/location/ABC.
Can I achieve this with nginx.conf?
P.S. I put http://domain.name explicitly in the rewrite rule because without it I was getting localhost in the browser, and my browser's localhost is not the same as the server's.
Rewrites issue redirects for browser.
If you just want to mount several locations from upstreams - you do not need rewrites, just use:
location /App1/ {
proxy_pass http://localhost:9001/;
}
But apps should use relative links or account for their absolute location.
For more complex url manipulation you can use break-rewrites:
location /App1/ {
rewrite ^/App1/(.*) /$1 break;
proxy_pass http://localhost:9001;
}
Is there an equivalent of apache's ProxyRemote directive for NginX?
So the scenario is I am behind a corporate proxy and I want to do proxy passes for various services with NginX. I would do it in Apache with the following:
ProxyPass /localStackOverflow/ https://stackoverflow.com/
ProxyPassReverse /localStackOverflow/ https://stackoverflow.com/
ProxyRemote https://stackoverflow.com/ http://(my corporate proxy IP)
I know I need the proxy_pass directive in NginX but can't find what I would use for the ProxyRemote.
Thanks
Not sure how #tacos response can work - possibly something I'm missing but the only way I could sort of get this to work was by rewriting the url and passing on to the corporate proxy. This is shown below:
http {
server {
listen 80;
location / {
rewrite ^(.*)$ "http://www.externalsite.com$1" break;
proxy_pass http://corporate-proxy.mycorp.com:8080;
}
}
}
This works, but does rewrite the url, not sure if this is important to the original use-case..
The servers you proxy behind an Nginx front-end web server are referred to as upstream servers. You will want to refer to the documentation for the HttpUpstreamModule. It's very similair to what you are familiar with. If you don't need load-balancing, you just setup the one upstream server in the configuration and it will serve your purpose.