301 Redirect on nginx machine running non-standard port behind a proxy - redirect

I have an nginx server processing PHP requests, but it's configured to listen on a non-standard port (port 12345 or something). I can't change the listen port because corporate IT says, "No."
There is a proxy in the data center that forwards requests from www.domain.com:80 to the nginx box on port 12345.
I have some static 301 redirects that I need to put in place, but I'm getting unexpected behavior.
Sample redirects in site.conf "server { }" block:
rewrite ^/foo$ /bar/foo/ permanent;
When I attempt to go to www.domain.com/foo, the redirect happens, but it tries to forward the browser to www.domain.com:12345/bar/foo/
My question is, how can I get nginx to redirect the user to the correct port (www.domain.com/bar/foo/)?
Maybe a better question is, what is the correct way to do what I'm asking? There are 50+ redirects that need to go in, and I'd rather not create a "location" section for each of those redirects.

You can provide a more explicit rewrite. Try the following:
rewrite ^/foo/ $scheme://www.domain.com:80/bar$request_uri permanent;
I have assumed that you meant to use ^/foo/ instead of ^/foo$, since ^/foo$ is a very specific case. Just revise as needed.

Related

What kind of redirection Type is dyndns using?

So i'm curious what kind of redirection dyndns.org ist using.
We used to have an Account there but we cancelled it, we build a Inhouse IP collection solution.
BUT:
Some host urls XY.dyndns.org where used for VPN (unifi l2tp) connections and i can't figure out how i have to setupt the php or .htaccess for redirecting like dyndns.org urls. dyndns was host with ip setting
what i have:
danymic IP
some Domain vpn.domain.com which gets my external IP from Modem at home (domainserver is in some Datacenter)
solution must be done with JS, PHP or .htaccess
what i want is:
vpn.domain.com is used as VPN Target Server Domain
This Domain should redirect to my external IP from Home which is the real VPN Access Point. When i use the IP directly the VPN tunnel is created properly.
i already tried with php header location and .htaccess
RewriteEngine on
RewriteRule (.*) http://10.125.111.111/$1 [R=301,L]
so i'm out of knowledge, please bring me in right direction.
what i
Tanks
As far as I could research, I would need some root server with the ability to change the IP Routing Tables. My 0815 Webserver isn't the right the choice for this.

Nginx/Django SSL Configuration for a specific sub page

I have an nginx/gunicorn/supervisor/posrgresql server with Django. I server two web pages from this server, with each with its own nginx conf file. I have purchased and downloaded SSL certificates and they are working in some circumstances.
My nginx conf file looks like this:
server {
listen 80;
server_name name.org;
return 301 https://www.name.org;
{
server {
listen 80;
listen 443 ssl;
server_name www.name.org;
ssl_certificate...
}
And so on. What I want to happen is this:
Either
1.) entire site with all pages https
or
2.) one particular sub-page with https, e.g. https://www.name.org/page, no matter how you get there, name.org/page, https://www.name.org/page, www.name.org/page, internal links, etc. The point is I need to serve THAT page over SSL.
Currently, name.org takes you to my home page https. www.name.org does not. I can go to any page on my site, and then enter that page name in the address bar, like https://www.name.org/anypage, and it will reload with the green lock.
I've been looking on stackoverflow, nginx documentation, godaddy (where I purchased my certificates) and everywhere else I can think to search for hours and cannot find anything, so any help will be quite welcome.
Change your first server block to include the www subdomain:
server {
listen 80;
server_name name.org www.name.org;
return 301 https://www.name.org;
}
This will redirect both http://name.org and http://www.name.org to https://www.name.org. After you've done that, you can remove the listen 80; from your second server block, as the first one covers that name / port combination, and you'll only be dealing with the ssl version from then on.

How to debug Json between Local IIS Website and Local

I'm really struggling to know what the problem is here...
I have 2 websites configured in IIS...
**PH.Website** (STANDARD MVC PROJECT)
**PH.Api** (MVC WEB API PRIOJECT)
When you hit the PH.Website it uses a WebClient class in one of the controllers to download data from the PH.Api. However fiddler only sees traffic going to the PH.Website not the PH.Api.
After reading around on the internet I apparently have to do some configuration of the Syste.Net in the Web Api Project. So I added...
<system.net>
<defaultProxy>
<proxy
usesystemdefault="False"
bypassonlocal="True"
proxyaddress="http://PH.Api"
/>
</defaultProxy>
</system.net>
But still no luck. I know the request is happening I just cant see it.
Whats even more confusing is when I configure Fiddler to use Port 80 its says it cannot listen on that port. But hang on if it cant listen on 80 then how the hell can it see traffic on the Http port:80 anyway?
P.s. I really need to be able to run multiple sites locally so. I hate running sites under Localhost or 127.0.0.1 seems so silly when you can create seperate sites for everything in IIS and know exactly where everything maps....
Let's back up.
Fiddler's a proxy server; it sees all of the traffic that is sent to it. Generally speaking (unless you're using it as a Reverse Proxy) you never want to configure Fiddler to run on port 80; instead leave it up at port 8888 where it runs by default. You instead configure your client to proxy its traffic through Fiddler.
Now, what "reading around on the Internet" did you do that caused you to modify your machine.config or web.config file (you didn't mention which you edited)?
If your goal is to watch traffic with Fiddler, you need to point the ASP.NET proxy settings at Fiddler, not at whatever "PH.api" is (e.g. use "127.0.0.1:8888" and set bypassOnLocal to false). The further complexity arises in that System.NET bypasses the proxy for any request to "localhost" or "127.0.0.1", so if you're using those addresses for your target, you should change them to "localhost.fiddler" temporarily while debugging.

can the different hosts (not ip) forwarding to the same port externally?

Im just wondering, can 2 or more different external hostname/DNS redirect to multiple local servers but same port?
Let's see, I have 2 DNS internet domain for an example, myserver1.com and myserver2.com, and both I have same A record to my forwarded server IP (e.g: 102.123.123.123). Under my server which only has 102.123.123.123 IP address has 2 application servers but instead of trying to make they work, I use different port for each server applications for an example, serverApp1 listening to 0.0.0.0:2010, serverApp2 listening to 0.0.0.0:2020
My point is, is there any way or how to forward my myserver1.com:2000 to serverApp1 (port 2010), and myserver2.com:2000 to serverApp2 (port 2020) but both myserver1.com and myserver2.com has a same A record?
Im quite sure either it is in iptables or /etc/hosts or BIND issues, but guide me if I missed something. And by the way, the servers and DNS records are accessible from the internet which is the firewalls are configured properly. Thanks.
I don't have much experience in that, but I think you will need a third server/firewall/proxy listening for the incoming host and route it accordingly.
Again, I don't have much experience in that, so I'm not sure if the firewall is able to do that.
I think you can use redirection servers like apache.
In my application we want to access lot of intranet servers from internet. So what we did, we configured a apache with all the mappings in httpd.
So when ever a request to apache comes, it will be redirected appropriately.
For example - I have two servers or hostname in intranet : 1) abc.com:7300/context1
2) xyz.com:8900/context2
We configured a apache with host name abcxyz.com:9000. When a request like
abcxyz.com:9000/context1 comes it will be redirected to abc.com:7300/context1 and when a request like abcxyz.com:9000/context2 comes it will be redirected to xyz.com:8900/context2.
In your case since the requests are going through the single server (102.123.123.123), you can use redirection.
Hope it helps.

With nginx, can I redirect the user back to a URL on their local machine?

For a particular internal purpose I would like to send people back to a url on their own machine, how exactly would I do this? I can't really do
server {
server_name www.yayaya.com;
rewrite ^(.*) localhost:3000$1 permanent;
}
because that will point to the server's localhost, right?
either it does the lookup on the server and converts "localhost" to "127.0.0.1"
or it hands "localhost" to the client, which will still convert it to "127.0.0.1"
So either way, the client should be redirected to 127.0.0.1, which should be
correct.
I'm not an expert on nginx but I don't see why your example wouldn't work.