Nginx subdomain: redirecting to wrong directory - nginx

I have a domain (let it be example.com). Also, I have a configured nginx web server, where example.com is pointed to the root directory /var/www/example.
Then, I wanted to point service.example.com to /var/www/example, and then point the original example.com to /var/www/service
Fisrt lines of nginx config for service.example.com look like this:
server {
server_name service.example.com;
root /var/www/example;
......
It works, and when I try to access service.example.com, I'm being redirected to the index file in /var/www/example.
Then, I made a config for example.com:
server {
server_name example.com www.example.com;
root /var/www/service;
......
,but when I'm trying to access example.com, I see the index file of /var/www/example— as if I tried to access service.example.com.
So, how can I solve this problem, and see index file of /var/www/service when I'm trying to access example.com?

Problem was in symlinks.
Some of them was not the symlinks in sites-enabled, but archive. So, nginx didn't handled them.
Problem was solved!

server {
root /var/www/service;
server_name service.example.com;
}
reference: https://hackprogramming.com/how-to-setup-subdomain-or-host-multiple-domains-using-nginx-in-linux-server/

Related

nginx: 502 bad gateway if /index.html is not in URL

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).

Nginx URL Redirection

I have a website running on port 80 as a default_server, its root in the config file is root /var/www/sample1/current and I can access it through www.sample1.com, and I have one more website on the same server:
/var/www/sample2/current
So I need to call the second website /var/www/sample2/current through www.sample1.com/sample2 on the same port.
You would want to use the http://nginx.org/r/alias directive.
root /var/www/sample1/current;
location ^~ /sample2 {
alias /var/www/sample2/current;
}

All requests to same directory, regardless of domain - NGINX

WordPress Multisite
Nginx
WPMU Domain Mapper
(I think my question has a non-WP-specific solution.)
Domains that point at my server's IP address don't get sent to my WordPress site unless they are defined in the config file or the domain is defined in /sites-enabled
So, if I just set the A-record of some random example.com domain to my IP, I get a "Welcome to nginx on Debian!" message.
But I need it to go directly to the WordPress directory.
And I need it to happen anytime anyone sets up a domain to point at the IP address. That is --- I need to set up the config so all requests go to the WordPress directory.
EDIT:
I have tried to set the default directory to /var/www/wordpress --- but that didn't seem to work.
You can use regex in nginx/conf.d/ config, to catch domains:
server {
listen 80;
set $location_root "/var/www/";
server_name ~^(?<somedomain>[\w-\.]+\.com)$;
root $location_root/$somedomain;
include conf.d/wpconf;
}
If you have domains not only in *.com area, just fix regex. For example, for domain site.com, this regex will go to /var/www/site.com directory.
Note: including conf.d/wpconf is not necessary, because i store in that file specific options for WPsites.
The solution was simple:
server {
listen 80 default_server;
server_name myprimarydomain.com *.myprimarydomain.com;
server_name_in_redirect off;
root /var/www/wordpress;
.
.
.
If you followed the Nginx/Multisite guide from WPMU --- you also have to remove the default file in the sites-available directory. It is being included by a line in the confd file, and it contains a conflicting default_server line.

nginx with 2 domains pointing to same root

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;
}

Struggling with location blocks in nginx config

I got a new slice off slicehost, for the purposes of playing around and learning nginx and more about deployment generally. I installed a ruby app on there (which i'll call app1) which uses passenger. I made it the default app to use for that server with the following server block in my nginx config:
server {
listen 80;
server_name <my server ip>;
root <path to app1 public folder>;
passenger_enabled on;
}
This works fine. However, i want to try a few different apps out on this slice, and so thought i would set it up like so:
http:///app1
http:///app2
etc. I thought i would be able to do that by adding a location block, and moving the app1 specific stuff into it like so:
server {
listen 80;
server_name <my server ip>;
location ^~ /app1 {
root <path to app1 public folder>;
passenger_enabled on;
}
}
However, on doing this (and restarting nginx of course), going to the plain ip address gives the 'welcome to nginx' message (which i'd expect). But, going to /app1 gives an error message:
404 Not Found
The requested URL /app1 was not found on this server.
This is distinct from the error message i get when i go to another path on that ip, eg /foo:
404 Not Found
nginx/0.8.53
So, it's like nginx knows about that location but i've not set it up properly. Can anyone set me straight? Should i set up different server blocks instead of using locations? I'm sure this is simple but can't work it out.
Cheers, max
What you're after is name virtual hosting. The idea is that each domain is hosted on the same IP, and nginx chooses the virtualhost to serve based on the Host: header in the HTTP request, which is sent by the browser.
To use name virtual hosting, use the domain you want to serve instead of your server's IP for the server_name directive.
server {
listen 80;
server_name app1.com;
location / {
root /srv/http/app1/public;
passenger_enabled on;
}
}
Then, to host more apps on the same box, just declare a separate server { } block for each one.
server {
listen 80;
server_name app2.com;
location / {
root /srv/http/app2/public;
passenger_enabled on;
}
}
I'm using unicorn instead of passenger, but the vhost part of the structure is the same for any backend.
The global nginx config (which on its own hosts nothing): https://github.com/benhoskings/babushka-deps/blob/master/nginx/nginx.conf.erb
The template wrapper for each virtualhost: https://github.com/benhoskings/babushka-deps/blob/master/nginx/vhost.conf.erb
The details of the unicorn virtualhost: https://github.com/benhoskings/babushka-deps/blob/master/nginx/unicorn_vhost.common.erb
I fail to see the real problem here tho,
in order for you to figure that out
you need to view the nginx log files on most systems at:
/var/log/nginx/
and open the relevant access file here(might be error.log)
in there you can see what url nginx exactly tried to access and why did it fail.
What I really think is happening, that you got the root path wrong,
maybe it should be alias instead because
if you are proxifying the connection to another app, it might get the
"app1" word in the url instead of a direct one.
so please try:
server {
listen 80;
server_name <my server ip>;
location /app1 {
alias <path to app1 public folder>;
passenger_enabled on;
}
}
and see weather it works and also try to view the logs first to really determine whats the problem.
I think its just a slight syntax problem:
location ~ ^/app1 { ...
should work, or a little more efficient:
location = /app1 { ...
One problem is that your Rails app probably wasn't designed to run from a subdirectory. Passenger has a directive that will fix this:
passenger_base_uri /app1;
However, running Rails apps in subdirectories is somewhat non-standard. If you can, a better option may be to set up subdomains using nginx's virtual hosts.
It seems that you want to host more apps on the same server with base uri. Try this:
root /srv/http/;
passenger_base_uri /app_1;
passenger_base_uri /app_2
Also under /srv/http, create 2 symlinks:
ln -s /srv/http/app_1 /srv/http/app1/public
ln -s /srv/http/app_2 /srv/http/app2/public
The app1 can be accessed under: http://domain.com/app_1.
Here is more for reading: http://www.modrails.com/documentation/Users%20guide%20Nginx.html#deploying_rack_to_sub_uri

Resources