Nginx Not Showing Static Content on Localhost - nginx

I'm working on creating a basic Nginx server to show a static HTML webpage (for now) and I am having an issue viewing my content. I've followed the tutorial here, by creating a new server block, named quake.dev. I have removed the symlink to the default server block in /etc/sites-enabled/default and created the symlink between sites-enabled and sites-available for quake.dev
My nginx.conf
user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;
events {
worker_connections 768;
# multi_accept on;
}
http {
##
# Basic Settings
##
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
# server_tokens off;
server_names_hash_bucket_size 64;
# server_name_in_redirect off;
include /etc/nginx/mime.types;
default_type application/octet-stream;
##
# SSL Settings
##
ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
ssl_prefer_server_ciphers on;
##
# Logging Settings
##
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
##
# Gzip Settings
##
gzip on;
gzip_disable "msie6";
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
quake.dev
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
root /var/www/quake.dev/html/;
# Add index.php to the list if you are using PHP
index index.html index.htm;
server_name quake.dev www.quake.dev;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
allow 192.168.0.1/24;
allow 127.0.0.1;
deny all;
}
}
I then added quake.dev to my /etc/hosts file:
hosts
#127.0.0.1 localhost
127.0.0.1 quake.dev
127.0.1.1 wintermute
# The following lines are desirable for IPv6 capable hosts
::1 ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
After restarting both nginx and the networking service, I load quake.dev into Chrome and it returns ERR_CONNECTION_REFUSED. Is there anything anyone can think of? I've been up and down this issue for days now.

UPDATE: turned out there was a broken symlink between sites-enabled and sites-available. places dunce cap on head

Related

NGINX redirecting to default server page

I'm trying to deploy an app to an Ubuntu 20.04 server with NGINX. My static build files are under /var/www/html directory. The issue I'm getting is that, when accessing the website through the domain name, the default server page (blank, with one line of text detailing server info) shows instead of my static files.
I tried changing the server_name and root values in the config file under "sites-available" directory. No matter whether I use the domain name or the IP address, I still get the same result.
This is the config file under sites-available:
server {
listen 80;
listen [::]:80;
server_name example.com;
root /var/www/html;
location / {
proxy_pass http://localhost:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}
And this is the default nginx.conf file:
user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;
events {
worker_connections 768;
# multi_accept on;
}
http {
##
# Basic Settings
##
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
# server_tokens off;
# server_names_hash_bucket_size 64;
# server_name_in_redirect off;
include /etc/nginx/mime.types;
default_type application/octet-stream;
##
# SSL Settings
##
ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3; # Dropping SSLv3, ref: POODLE
ssl_prefer_server_ciphers on;
##
# Logging Settings
##
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
##
# Gzip Settings
##
gzip on;
# gzip_vary on;
# gzip_proxied any;
# gzip_comp_level 6;
# gzip_buffers 16 8k;
# gzip_http_version 1.1;
# gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
##
# Virtual Host Configs
##
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
your question is off-topic here and should be asked on serverfault.
There are a number of things missing from your description which I would expect to see. While this does not point to a definitive cause for your issue, they may be contributory factors:
Regarding....
This is the config file under sites-available:
On Ubuntu, Nginx ignores this directory - it looks in /etc/nginx/sites-enabled for the specific sites to run. The files themselves should reside in /etc/nginx/sites-available and be symlinked from /etc/nginx/sites-enabled
If you want multiple sites on your server then you should have at least 2 files visible in /etc/nginx/sites-enabled, a default one like this....
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
root /var/www/html;
...
(Note the "default_server" and "server_name _") and one for each named server.
IME, Nginx needs to load the default config first. And it loads the files in alphabetical order. So a symlink named "a.example.com" will be loaded before "default". On my servers, the default config is named "_default"

Static location with nginx downloading file instead of showing response

I've read about this problem with php a numerous amount of times by now. I'm trying to get the basic of nginx since I almost exclusively used apache or iis.
I'm running a small debian 9 server and trying to figure out the basics of the nginx config.
the nginx.conf file is mostely untouched by me.
user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;
events {
worker_connections 768;
}
http {
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
ssl_prefer_server_ciphers on;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
gzip on;
gzip_disable "msie6";
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
The default.conf file however is edited by me:
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name 10.20.30.1;
root /var/www;
index index.html index.htm index.nginx-debian.html;
}
when accessing via IP, I get everything from my /var/www folder correctly loaded up with css files. So the including mime-type is working quite well.
after adding:
location /greet {
return 200 "Hello User!";
}
to the server configuration I'm able to access http://10.20.30.1/greet but instead of displaying the message within the browser it's being downloaded as a file without extension, called greet with my message inside.
I found many simular problems like mine regarding php. In this case php shouldn't even be an issue by now.
Any advice would be very helpful.
thank you in advance!
That return statement literally sends a text response with the default content type. If the browser does not understand how to present a given content type, it will offer to download the file.
You can tell the browser that it's plain text using the default_type directive.
For example:
location /greet {
default_type text/plain;
return 200 "Hello User!";
}

Ubuntu 18.04 Nginx - always only shows the welcome page

I am using Ubuntu 18.04 default nginx configuration, with some minor changes. But I always sees the nginx's default welcome message. All config files follows:
/etc/nginx/nginx.conf:
user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;
events {
worker_connections 768;
# multi_accept on;
}
http {
##
# Basic Settings
##
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
# server_tokens off;
# server_names_hash_bucket_size 64;
# server_name_in_redirect off;
include /etc/nginx/mime.types;
default_type application/octet-stream;
##
# SSL Settings
##
ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
ssl_prefer_server_ciphers on;
##
# Logging Settings
##
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
##
# Gzip Settings
##
gzip on;
# gzip_vary on;
# gzip_proxied any;
# gzip_comp_level 6;
# gzip_buffers 16 8k;
# gzip_http_version 1.1;
# gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
##
# Virtual Host Configs
##
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
/etc/nginx/conf.d/default.conf:
This file is empty. And this directory only contains one file.
/etc/nginx/sites-enabled/default:
upstream backend {
server 127.0.0.1:8068;
}
server {
listen 80;
listen [::]:80;
# server_name _;
location / {
proxy_pass http://backend;
}
}
On the server, the http server runs correctly:
$ curl 127.0.0.1:8068
some contents...
In another computer, only the welcome message shows:
$ curl http://the-server-ip-address
the default welcome to nginx message follows...
I've also tried to access the ip address in my Chrome and disabled caches, but the same welcome page always shows.
/var/log/nginx/access.log and /var/log/nginx/error.log shows no access logs and no errors.
Does my nginx config files have a problem? Thanks!
(I just tried the same config files on a 16.04 ubuntu server and it works correctly.)
I've run the follow commands to test and reload the configs:
$ nginx -t
$ nginx -s reload
updated
Sorry, I just tried some other operation:
I run the following command on this server:
$ ifconfig
and get the ip address is 192.*** rather than the ip address I used to access(ssh to) this server.
I run
$ curl 192.***:80
and the server responds correctly.
But why the ip address I used to ssh to the server always shows the welcome page?
$ curl the-server-ip-address-for-ssh:80
still the welcome page...
Thanks!

How to configure the sites-available file for a specific site in nginx for a Laravel application?

I'm trying to deploy a Laravel 5.4 application through Nginx. So, I'm more or less following this tutorial.
My server is an Amazon EC2 running Ubuntu 16.04 with PHP 7.0. Nginx version is the 1.10.3.
At the current moment, I do not have a domain to my application, so I can only access it through the ip. I want to access my application through an URL similar to the following:
http://ec2-xxx-xxx-xxx-xxx.us-west-2.compute.amazonaws.com/my-site
However, trying to do it shows an error 403.
If I try to access directly the public folder (http://ec2-xxx-xxx-xxx-xxx.us-west-2.compute.amazonaws.com/my-site/public), I'm receiving the home page of my application, but the links are broken. Since I got a Laravel error when I did a mistake, it seems to be working.
The port 80 is open (by this answer), and if I simply create a folder inside /var/www/html and put an index.php file I can access it through the browser. Trying to access http://ec2-xxx-xxx-xxx-xxx.us-west-2.compute.amazonaws.com shows the Welcome to nginx default page, so nginx is working.
Artisan (php artisan serve) it seems to work in the terminal, but when I try to access it through the browser (http://ec2-xxx-xxx-xxx-xxx.us-west-2.compute.amazonaws.com:8000), connection is refused (and I opened the port 8000 too).
Trying to access directly through the ip () results in the same behavior for every URL (xxx.xxx.xxx.xxx shows nginx welcome, xxx.xxx.xxx.xxx/my-site returns a 403 error etc.)
I think my problem is with the sites-available files. I'm not sure how to properly name the file for my specific application, and I feel this is the problem - so nginx is not able to identify the file and so apply the configurations in the site:
Without the comments, here is my /etc/nginx/sites-available/default file:
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html;
index index.php index.html index.htm index.nginx-debian.html;
server_name xxx.xxx.xxx.xxx;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}
location ~ /\.ht {
deny all;
}
}
And this is the content of the /etc/nginx/sites-available/my-site file:
server {
listen 80;
listen [::]:80;
root /var/www/html/my-site/public;
# Add index.php to the list if you are using PHP
index index.php index.html index.htm index.nginx-debian.html;
server_name xxx.xxx.xxx.xxx/my-site;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}
location ~ /\.ht {
deny all;
}
}
And here is my nginx.conf file content:
user www-data;
worker_processes auto;
pid /run/nginx.pid;
events {
worker_connections 768;
# multi_accept on;
}
http {
##
# Basic Settings
##
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
# server_tokens off;
# server_names_hash_bucket_size 64;
# server_name_in_redirect off;
include /etc/nginx/mime.types;
default_type application/octet-stream;
##
# SSL Settings
##
ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
ssl_prefer_server_ciphers on;
##
# Logging Settings
##
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
##
# Gzip Settings
##
gzip on;
gzip_disable "msie6";
# gzip_vary on;
# gzip_proxied any;
# gzip_comp_level 6;
# gzip_buffers 16 8k;
# gzip_http_version 1.1;
# gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
##
# Virtual Host Configs
##
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
P.s.: I already created the symbolic link for the sites-enabled folder.
I tried to use the EC2 domain (ec2-xxx-xxx-xxx-xxx.us-west-2.compute.amazonaws.com in the server_name of both files, but it returned an error when I tried to restart nginx.
Removing the /my-site part of the my-site file server_name still returned a 403 error in the ec2-xxx-xxx-xxx-xxx.us-west-2.compute.amazonaws.com/my-site URL. However, if I remove the default nginx welcome file (index.nginx-debian.html), I am able to access the intended home page by the http://ec2-xxx-xxx-xxx-xxx.us-west-2.compute.amazonaws.com URL, without the my-site in it. Links are working as well; however, JS files both external and inside the same ip are not being loaded due to "Content Security Policy", and the URL is not the one I intended.
So, is there something wrong about these configurations, specifically about the server names and the name of the file?

vhost and dns with nginx

i setup a virtualbox with a clean install of ubuntu 16. installed nginx, php7, mysql all fine. the computer name is: mercury
i've setup a folder that would be the root for all my web projects: /var/www
i want to be able to have a dynamic virtual host where i can just create a folder (like: /var/www/project1) and i'll easily be able to access it via the browser at: project1.mercury
i can i achieve this? in my browser: mercury/ loads up fine (/var/www/index.html), but as soon as I use a subdomain it craps out and gives me a dns error: server DNS address could not be found
here is my: /etc/nginx/nginx.conf file
user www-data;
worker_processes 4;
pid /var/run/nginx.pid;
worker_rlimit_nofile 30000;
events { worker_connections 1024; }
http {
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
server_tokens off;
include /etc/nginx/mime.types;
default_type application/octet-stream;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
proxy_connect_timeout 60;
proxy_read_timeout 60;
proxy_send_timeout 60;
gzip on;
gzip_disable "msie6";
gzip_types text/plain text/xml text/css text/comma-separated-values;
upstream app_server {
server 127.0.0.1:8080 fail_timeout=0;
}
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
/etc/nginx/sites-enabled/default:
server {
listen 80;
server_name ~^(?P<subdomain>.+)\.mercury$;
location / {
root /var/www/$subdomain;
}
}
UPDATE:
so this config works, but i need to manually update my Windows hosts file and specify the subdomain with the same ip address:
192.168.1.101 project1.mercury
if i dont, i get a dns error.
how can this be achieved without having to manually add an entry in the hosts file everytime??

Resources