I'm happy to join community and want to share with you with my little problem.
I've got wildcard entry for example.com in my DNS which points all subdomains to some machine
* IN A 172.172.172.172
While NGINX configuration for this domain contains only actively used subdomain names
server {
listen 10.0.0.1:80;
server_name example.com www.example.com
moskva.example.com www.moskva.example.com
tokyo.example.com www.tokyo.example.com;
...
}
What I want to achieve is directing all unconfigured subdomains like 'mistake.example.com' to specific address.
Is there any elegant way of solving this problem?
Best Regards
Arek
This will instruct the site to redirect any unmatched traffic to example.com
server {
listen 80 default_server;
return 301 http://example.com;
}
You can set a default server section like this:
server {
listen 10.0.0.1:80 default_server;
server_name _;
...
}
Related
I'm working with Nginxand I want to know how can I redirect all request with a specific port to a subdomain ?
This is my default.conf :
server{
listen 80 default_server;
server_name localhost;
location / {
root /usr/share/nginx/html;
}
}
server{
listen 80;
server_name blog.mydomain.com;
location / {
proxy_pass http://my-ip:8080;
}
}
So with this I have the default mydomain.com serve the html folder, and a subdomain blog.mydomain.com serve an application running port 8080.
My problem is when I try to access directly my-ip:8080, or mydomain.com:8080 or blog.mydomain.com:8080 the server serve the application running port 8080 and I want to redirect all these requests to blog.mydomain.com without the :8080.
How can I do that ? Automatically redirect to blog.mydomain.com if I specify :8080 in the url ?
When you use my-ip:8080 or mydomain.com:8080, you call directly your application, not nginx. The issue I see here is not "my webapp doesn't redirect to nginx" but "I have a proxy but my webapp is directly accessible from the internet".
A clean way to fix it is to hide your application from the internet and let nginx be the only way to contact it. Configure your application to listen on localhost (it looks like it listens on * or 0.0.0.0, meaning all interfaces). Then, tell nginx to proxy requests to http://localhost:8080;.
If you really want to keep your application listening on *:8080, you can add a HTTP header in nginx (like X-Forwarded-For with proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;) and detect it in your application. Without it, redirect to nginx. Note that anyone can add HTTP headers...
There is two solutions for your problem:
If you can change code of application then you can handle HTTP request in application. You should be able get information about port from headers.
If you cannot change code of application but you can change port on which application is listening, then you can do redirect in nginx. To do this, you need start listening on port 8080 in nginx and redirect it to 80, and on 80 port you change proxy_pass to new port of application.
I am using a block to redirect all http traffic to https. Simple stuff. However, if I address the server directly by IP, it always redirects to the server_name given. If I do not give a server_name, it does not redirect. This is highly undesirable, as I may be on a LAN where a domain name would go unresolved. Here is my redirect block:
server {
listen 80 default_server; ## listen for ipv4; this line is default and implied
return 301 https://$server_name$request_uri;
}
It redirects to server_name because you set it up like that.
Look at your return 301 https://$server_name$request_uri;. Either you replace $server_name by $host or by the IP you want to redirect to.
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.
Lets say I have a website named xyz.co, I also have other domain names with the same prefix like xyz.com, xyz.it, xyz.co.it
Right now nginx works fine with server_name xyz.co in nginx.conf in port 80 I would want all the other domains to redirect to xyz.co also I would want www.* versions of the above to redirect to xyz.co. How can I get this? Is this nginx webserver level changes? or I need to make this changes in DNS?
UPDATE: I tried this in nginx.conf but no use...
server
{
listen 80;
server_name xyz.co xyz.com, xyz.it, xyz.co.it;
rewrite ^/(.*) http://xyz.co permanent;
}
I first tried posting this question in ServerFault but no response there - https://serverfault.com/questions/453472/nginx-domain-name-redirects
add one server block for all the domain names that need to be redirected. like this:
server {
listen 80;
server_name xyz.com, xyz.it, xyz.co.it;
rewrite ^ http://xyz.co$request_uri permanent;
}
and another server block for the xyz.co domain:
server {
listen 80;
server_name xyz.co;
#other settings
}
this way when you go to one of the domain names that need to be redirected nginx will simply redirect to xyz.co and move into the other server block where you can add all your settings (rootfolder, location blocks, etc)
i have nginx on port 8080 sitting behind varnish running on port 80. there is only one website on my server. the problem is you can access it by server's IP address too, instead of just url. google indexed this ip and i am afraid of problems with duplicate content.
how do i redirect requests going to IP address to my URL? i tried this code, but it ended up with loop redirects error.
server {
listen 180.10.1.1:80;
server_name 180.10.1.1;
rewrite .* http://www.mysite.com$request_uri permanent;
}
thanks
edit:
rest of vcl
server {
listen 8080;
server_name site.com;
access_log /var/log/nginx/localhost.access.log;
error_page 502 /502.html;
## Default location
location / {
root /home/site.com/public_html;
index index.php;
...
There's a couple of ways to solve this. If Nginx is also serving site.com when you visit the server IP Address then you should adjust the Nginx config so any requests which are directed at the IP address redirect to site.com. Then restart both Nginx and Varnish.
ok the problem was "180.10.1.1:" in the listen directive. i kept there only "listen 80" and now it works fine :)