I'm a noob in nginx. I start configuring my domains (marianamarques.ntr.br and fabricadevozes.com.br) dns to point to my aws ec2 instance public ip. Thats ok.
When i start configure the nginx:
i created /var/www
i created /var/www/marianamarques.ntr.br/public_html
i created /var/www/fabricadevozes.com.br/public_html
i created /etc/nginx/sites-availble/marianamarques.ntr.br.conf listening to port 80 with root pointing to /var/www/marianamarques.ntr.br/public_html
i created /etc/nginx/sites-availble/fabricadevozes.com.br.conf listening to port 80 with root pointing to /var/www/fabricadevozes.com.br/public_html
When i tell on browser http://www.fabricadevozes.com.br i got htmls from /var/www/fabricadevozes.com.br/public_html but when i tell on browser http://www.marianamarques.ntr.br i got htmls from /var/www/fabricadevozes.com.br/public_html too.
I'm a bit desperated. My nginx was installed from apt-get and after hours and hours of web searches i know my /etc/nginx/conf.d/nginx.conf was missing (i don't have this file) but my nginx server starts with no issues.
Anybody can help?
nginx.conf should be located in /etc/nginx.
Post it along with your config files in sites-enabled and it'll be easier to tell you exactly what's wrong, but sounds like you may have a mistake in the server_name or root directives in your server definition. Make sure you specify the server name with and without the www. It could be loading your default domain if you didn't specify www. in the server name
server {
listen 80;
server_name marianamarques.ntr.br www.marianamarques.ntr.br;
root /var/www/marianamarques.ntr.br/public_html;
...
}
If thats not it, we'd need to see the config files.
Solved based on the link Nginx no-www to www and www to no-www - i create a second server listening to same port but expecting the server_name with-www prefix and then rewrite this to my other server point to without-www prefix like this:
server {
server_name www.domain.com;
rewrite ^(.*) http://domain.com$1 permanent;
}
server {
server_name domain.com;
#The rest of your configuration goes here#
}
I expect it can help others. Thanks a lot for everything! And sorry... my english is very poor!
Apparently you solved the problem but let me explain why it happened, you should read How nginx processes a request
Quote:
In this configuration nginx tests only the request’s header field
“Host” to determine which server the request should be routed to. If
its value does not match any server name, or the request does not
contain this header field at all, then nginx will route the request to
the default server for this port. In the configuration above, the
default server is the first one — which is nginx’s standard default
behaviour. It can also be set explicitly which server should be
default, with the default_server parameter in the listen directive
When you had a www or a non-www site missing, nginx couldn't match it to any server so it sends the request to the default server, and assuming you removed the default file from sites-enabled and didn't set any as default then the default server is the first one alphabetically, if we compare marianamarques.ntr.br.conf vs fabricadevozes.com.br.conf then the winner is the one starting with an f, that's why it was showing that server instead.
And since you did the basic redirection server, let me add that you better have used return over rewrite, check taxing rewrites
server {
server_name www.example.com;
return 301 http://example.com$request_uri$is_args$query_string;
}
Related
i don't understand what i'm doing wrong so i hope somebody can help :)
When i access http://10.0.0.54/index.html i get the right page but if i try to access http://10.0.0.54 instead of showing the index file it redirects me to https://10.0.0.54 showing error 502 bad gateway.
This is the configuration /etc/nginx/sites-available/default
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html/salvaderi;
index index.html;
server_name _;
location ~ /.well-known/acme-challenge {
allow all;
root /var/www/html/salvaderi;
}
location / {
root /var/www/html/salvaderi;
index index.html;
}
}
I am running nginx 1.18.0 on ubuntu 22.04
i tried changing parameters inside location /{} but i always get the same result. I also changed the root directory and made sure permission where set right. Searching on for the solution i saw other people having problems about PHP and FastCGI but i am not using it.
Your configuration about to be right.
Possible there is some kind of proxy or load-balancer is placed between you and nginx you configuring since you got redirect to HTTPS whether there is no any redirection instructions in your config and, in the same time, there is no listen 443 ssl in config, but you still got response on HTTPS request.
I'd check next:
Is 10.0.0.54 in fact IP of your server?
Is there any return 301, return 302 or rewrite instructions in your nginx config (the better
way is to dump config with nginx -T command and look over).
Didn't
you previously have configured some redirects that may have been
cached by your web client previously? Try to send GET request with
curl instead of web browser (if browser been used for tests).
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.
I've recently been trying to set up a reverse proxy that would forward certain 4th-level subdomains to particular locations. So, for example, this is what I'm trying to accomplish (configuration in my nginx file):
server {
listen 80;
server_name *.server.domain.com;
rewrite ^ https://$server_name$request_uri;
}
The goal here being that if someone went to, for example, http://item1.server.domain.com, they would be re-routed to https://item1.server.domain.com. However, with this configuration, the URL gets rewritten to https://%2A.server.domain.com.
Is there a way to fix this so that the full domain (item1) gets added correctly to the rewritten URL? Ideally, I wanted it to eventually be able to rewrite any subdomain on server.domain.com directly to https.
Thanks!
The $server_name variable contains the text from the value of the server_name directive. The %2A is a URL encoded representation of the leading *.
Use $host or $http_host to obtain the hostname actually requested by the client. See this document for more.
For example:
server {
listen 80;
server_name *.server.domain.com;
return 301 https://$host$request_uri;
}
Note: Restart nginx and clear the browser cache between each test. Check the configuration using nginx -T.
I complete the configuration in nginx as well as i connect it via this link
http://1.2.3.4/MyProject/
where my ip address assign in nginx 1.2.3.4
and my nginx configuration
server_name 1.2.3.4;
listen 80;
but when i give domain name like
server name account.com;
listen 80;
but when i open that page from browser like:- http://account.come it will redirect at http://:1.2.3.4/myproject
so how can i hide my ip-address and see my domain name?
Thankyou so much.
remove any server_name 1.2.3.4; in all config files and also see default config file under site-availeables
to check why its redirect to ip please check yor DNS server for example bind or bind9 service or named: /etc/named and all zones under /var/lib/named
maybe new site not configured proper by you then ping sitename once in your client system like windows and once by lynx on server via putty...
2-check nginx is working with proxycache or module cache ??
3-check your project code setting for example wordpress or some projects redirect some pages to cached url on database...
to check is from this project make test.php and code about echo rand(1,999); that can test redirection and cache at once.
ifrandom code show same number it is cache problem.
if no cache but redirecting to server ip address it is configuration issue...
if not solved remove all http server configs and define again .
add with www alias for site name
server {
listen 80 ;
server_name a.com www.a.com;
I had got the solution for that, I changed my godady account configuration.
and then put like:-
server_name abc.com;
I have my staging config setup like so:
server {
listen 80;
server_name staging.domain.com;
root /var/www/staging/public;
and my production config setup like this:
server {
listen 80;
server_name www.domain.com;
root /var/www/production/public;
With no other redirects or anything.
The issue is that even if I disable the production config I can still access the staging server at www.domain.com.
Why is it not being restricted to its configured subdomain?
I've answered a similar question like this before
Let me start with a small explanation on how nginx matches the hosts, quoting from how nginx processes a request
In this configuration nginx tests only the request’s header field
“Host” to determine which server the request should be routed to. If
its value does not match any server name, or the request does not
contain this header field at all, then nginx will route the request to
the default server for this port.
When you disable the main server you only have 1 left, so nginx passes the request to it, if you want to avoid that you need to add a main server to block all unconfigured domains
server {
listen 80 default_server;
return 403;
}
Then run
sudo service nginx reload
Then you're set