nginx shows vhost instead of default root - nginx

I just installed ngninx on my dev machine.
It automatically migrated my vhosts from lighttpd (very comfy!), I only had to adjust the TLDs (it only took \.dev, I changed that to \.(dev|test|local).
and bound itself to port 81; after removing lighttpd, I changed the ports in /etc/nginx/sites-available to 80.
But when I call http://<ip-adress>/ in the browser, I get the index page of one of my vhosts instead of the default DOCUMENT_ROOT (/var/www/).
I touched /etc/nginx/sites-available/default, changed the port number and uncommented the PHP block.
current contents (comments stripped):
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www;
index.php index index.html index.htm index.nginx-debian.html;
server_name _;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass 127.0.0.1:9000;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}
}
Half of the vhosts had self-references in /etc/nginx/sites-enabled, I replaced them with symlinks to /etc/nginx/sites-available and added a symlink for default; all my vhosts can now be accessed, but calling the IP address still routes to the same vhost instead of /var/www.
That vhost file is neither alphabetically first nor considering the mtime, but it is when I list the directory unsorted (ls -f), it even comes before ...
How do I get nginx to deliver /var/www/ instead of /var/www/vhost/?
update: After a few clicks on my primary vhost, switching to https and back, it changed:
http://www.vhost1.test now routes to /var/www, but the other vhosts seem to work correctly.
update: I tried to solve the problem by uncommenting the server block in nginx.conv (pointing to /var/www) and linking sites-enabled/default to sites-available/vhost1. The latter resulted in both the ip-address and vhost1 getting routed to another vhost. The other vhosts are still working fine.

I got it:
sites-available/vhost1 only had listen 443 ssl;; listen 80; was missing
(because listen 80 default_server caused a "duplicate default server" error),
so calling the domain via port 80 fell back to the default server.

Related

Nginx redirects to unwanted port

I’m trying to host 2 different websites - one static non-wordpress site, and one wordpress subdomain site - on my own pi server (test sites). Whenever accessing the subdomain site test.mysite.co.uk, it instead loads test.mysite.co.uk:4323 at the unwanted port 4323. The main mysite.co.uk site loads correctly however.
Initially I’ve been running these test sites locally (on different ports - the main site on port 4321 and subdomain on 4323) until I decided to deploy them using real domain names. However, presumably you cannot configure DNS to point to a specific IP and port (presumably a DNS record just points to an IP only), so I changed both the 2 domains’ conf files to listen to port 80 (as apparently you can define the server names to tell nginx which site to load - called virtual hosts?). Note that I have DNS A records for mysite.co.uk and test.mysite.co.uk that both point to the same public IP address of my router.
Nowhere is there a reference to port 4323 anymore, so I am confused as to why the subdomain still insists on forwarding to that port. I’ve been using incognito mode on chrome so there should be no caching issues. My router forwards external port 80 to internal port 80, and I’ve restarted the nginx server multiple times. The default port of my pi itself is no longer 80.
Here’s the /etc/nginx/sites-available/mysite.co.uk.conf file:
server {
listen 80;
listen [::]:80;
root /var/www/mysite.co.uk;
# Add index.php to the list if you are using PHP
index index.html index.htm index.nginx-debian.html;
server_name mysite.co.uk www.mysite.co.uk;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
}
}
And here’s the /etc/nginx/sites-available/test.mysite.co.uk.conf file:
upstream wp-php-handler {
server unix:/var/run/php/php7.4-fpm.sock;
}
server {
listen 80;
server_name test.mysite.co.uk;
root /var/www/wp.mysite.co.uk;
index index.php;
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass wp-php-handler;
}
}
Both .conf files are also symlinked to their respective sites-enabled folders.
Not sure if this means anything but loading local_ip:80 (or without :80 as presumably it assumes :80) in a web browser returns the Apache2 Ubuntu Default Page.
As per this post, I’ve tried adding port_in_redirect off, autoindex on and proxy_redirect http://test.mysite.co.uk:4323/ http://test.mysite.co.uk/ but to no avail.
Does anyone have any ideas of what I’m doing wrong?
UPDATE:
I've managed to create another test non-wordpress site that's exactly the same as the first non-wordpress site but called copy.mysite.co.uk, which seems to work. I'm assuming the problem with the wordpress test site may be to do with its config (although I can't see anything wrong with the code I've listed here)?

How does nginx know my servers url adress?

I installed nginx using sudo apt-get install nginx.
Now this allows me to go to my_ip:port and it allows me to visit the website.
Yet, i can also do my_url:port and it will also direct me to the website.
How can nginx know my_url when I have not told it my_url anymore?
I was running Apache before, can that explain it?
Nginx was able to load via the fqdn my_url:port even though you haven't added my_url in the nginx config because config default_server (usually there by default) was specified.
default_server parameter specifies which block should serve a request if the server_name requested does not match any of the available server blocks:
For example
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html;
index index.html index.htm index.nginx-debian.html;
server_name _;
location / {
try_files $uri $uri/ =404;
}
}
Nginx doesn't need it (at least, not yet). Your web browser looks up my_url in the DNS, and then uses my_ip (from DNS) :port (which you entered in your browser) to connect to Nginx.
Your Nginx is probably only configured with one site, which means any connection to it - regardless of whether it is by IP or by domain name - causes Nginx to serve that site. You can change this by going into your Nginx configuration files and setting (or changing) the value of the server_name parameter, for example:
server { # You already have a server block somewhere in the config file
listen 80; # Or 443, if you've enabled SSL
server_name example.com www.example.com; # Add (or change) this line to the list of addresses you want to answer to

nginx server block not playing nicely - 'server not found'

The basic installation is working, on linux mint OS. resolving the domain on 'localhost' confirms that nginx is running.
however, the issue i am running into stems from the generation of my own server block. its very basic:
server {
listen 80;
listen [::]:80;
root /usr/share/nginx/html;
index index.html index.htm;
# Make site accessible from alias.
server_name tokum.com www.tokum.com;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ /index.html;
# Uncomment to enable naxsi on this location
# include /etc/nginx/naxsi.rules
}
}
as you can see, i have created an alias for www.tokum.com in this server block. attempting to resolve this url in a browser, i am greeted with the lovely 'server not found' message.
my feeling is that it surrounds the 'try_files' functionality, but i cannot be sure why.
No other resources have been created on the server other than my tokum.com server block file, which is located at the path /etc/nginx/sites-available/tokum.com. Any help is most appreciated.

simple nginx server not working

I am new to nginx environment and trying to host my first app using nginx.
But I am not being able start the first steps with nginx.
I have seen and read thousands of tutorials on basic nginx setup and have set up basic nginx server block as anyone would have.
Here is my sites-available/default
server {
listen 80 default_server;
listen [::]:80 default_server;
# SSL configuration
#
# listen 443 ssl default_server;
# listen [::]:443 ssl default_server;
#
# Note: You should disable gzip for SSL traffic.
# See: https://bugs.debian.org/773332
#
# Read up on ssl_ciphers to ensure a secure configuration.
# See: https://bugs.debian.org/765782
#
# Self signed certs generated by the ssl-cert package
# Don't use them in a production server!
#
# include snippets/snakeoil.conf;
root /var/www/html;
# Add index.php to the list if you are using PHP
index index.html index.htm ;
error_log /var/log/nginx/error.log debug;
error_page 400 401 402 403 404 40x.html;
server_name mydomain.com;
location / {
root /var/www/html;
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# include snippets/fastcgi-php.conf;
#
# # With php7.0-cgi alone:
# fastcgi_pass 127.0.0.1:9000;
# # With php7.0-fpm:
# fastcgi_pass unix:/run/php/php7.0-fpm.sock;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
location ~ /\.ht {
deny all;
}
}
And i have done several deployments with apache but with nginx, i am experiencing peculiar behaviour.
This is how it goes.
It serves the default nginx welcome page successfully from /var/www/htmlon mydomain.com
Now, if i create a new html file say test.html inside /var/www/html and try to open mydomain.com/test.html, it shows internal server error with no logs in error log or access log.
Now in my server block, if i add test.html to the index directive as the first option, the same /var/www/html/test.html file is served and seen without any error on mydomain.com (So that it is clear that there are no problems in file permissions).
Also, if say i have index as the default index page only, if i add a hyperlink in the default index page, say Test Page , and on the default home page served on mydomain.com if i click on that hyperlink, test.html file is served, but the url in my browser does not change.
I am banging my head on this from last two days and i have tried several things.
Increased the verbosity of error logs to debug, still nothing shows up in logs
Tried a hundred other logically same but syntactically different server configurations.
I am pretty experienced with server configurations and have done number of deployments with apache and have never experienced something like this on apache.
Maybe, I am skipping some of the basic concepts of nginx as i do not know much about nginx but felt it would be similar to apache.
Please help me with this issue.
Thanks in advance

fastcgi_mono_server 4 wildcard hostname

I have configured nginx with fastcgi_mono_server4.
In my nginx config I have 2 hostnames :
server {
listen 80;
server_name dev.example.org
location / {
root /var/www/dev.example.org/;
fastcgi_index Default.aspx;
fastcgi_pass 127.0.0.1:9001;
include /etc/nginx/fastcgi_params;
}
}
server {
listen 80;
server_name *.example.org
location / {
root /var/www/example.org/;
fastcgi_index Default.aspx;
fastcgi_pass 127.0.0.1:9000;
include /etc/nginx/fastcgi_params;
}
}
nginx is OK with this configuration. dev goes to one and all other to another one .
I've already tried this :
fastcgi-mono-server4 /applications=*.example.org:/:/var/www/example.org/ /socket=tcp:127.0.0.1:9000
but it throws an error (Uri parse exception)
Update :
I need to get the full host name in my application, for example if the request was abc.example.org, I need to get "abc".
Unfortunately, HttpContext.Current.Request.Url does not contains "abc" but "*" which causes the parse error
If nginx is going to take care of routing the appropriate sub-domains to each fastcgi port (9000 or 9001) then can you get away with a wildcard domain when you start the mono server process e.g. just use a * instead of '*.example.org'
fastcgi-mono-server4 /applications=*:/:/var/www/example.org/ /socket=tcp:127.0.0.1:9000
Update: The above works to get two Mono server apps listening via nginx, but, using the nginx config from the original question will lead to an exception if you call HttpContext.Request.Url on the catch-all server. This is due to it not liking the * in *.example.org.
There are two possible solutions, depending what you'd like to see returned from HttpContext.Request.Url when a client browses foo.example.org, bar.example.org etc.
Option 1: If you don't care about the sub-domain and want to see example.org
Configure the second (*.example.org) nginx server to be the 'default_server' and have it assign a server-name without the wildcard e.g.
server {
listen 80 default_server;
server_name example.org;
access_log ... }
With these settings, browsing to foo.example.org/Default.aspx loads the page and HttpContext.Request.Url returns example.org/Default.aspx
Option 2: If you want to see the actual sub-domain e.g. foo.example.org
Removing the server_name from the second server definition works.
server {
listen 80 default_server;
access_log ... }
With these settings, browsing to foo.example.org/Default.aspx loads the page and HttpContext.Request.Url returns foo.example.org/Default.aspx
#stephen's answer is more simple and does not need fastcgi config modification.
I tried previous answer (before update), but it did not work.
Nginx take care of routing, as #stephen said, and the routing part worked.
to start fastcgi I used this command to match all routes (and server names)
fastcgi-mono-server4 /applications=/:/var/www/example.org/ /socket=tcp:127.0.0.1:9000
The problem was that HttpContext.Request.Url contains the $server_name value in my case it was "*.example.org" and when I try to parse URI there was an error.
To handle this I changed nginx fastcgi_params and replaced thi line
fastcgi_param SERVER_NAME $server_name;
by
fastcgi_param SERVER_NAME $http_host;
and add in site-available conf
proxy_set_header Host $host;
I think it is set by default.
reload nginx
nginx -t && service nginx reload
reload fastcgi-mono-server to test
fastcgi-mono-server4 /applications=/://var/www/example.org/ /socket=tcp:127.0.0.1:9000 /printlog=True /loglevels=Debug
in the log SERVER_NAME contains the real (not *) subomain.

Resources