Static location with nginx downloading file instead of showing response - nginx

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

Related

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?

NGINX not serving CSS, Images and other media files

I have installed NGINX on my ubuntu 16.04 LTS server to satisfy the need to navigate to different applications on the same linux server.
So I have installed it and followed this tutorial : https://www.youtube.com/watch?v=PTmFbYG0hK4&t=677s
I defined it exactly as the tutorial shows but I ran into a problem where the NGINX not serving any media files for a specific application (CSS, Images, stylesheets etc). I will be clearer: I defined inside sites-available a configuration file as such (of course I made a symbolic link to the sites-enabled directory.):
server{
listen 80;
listen 443 ssl;
location / {
root /home/agent/lexicala;
}
location /test {
proxy_pass "http://127.0.0.1:5000";
rewrite ^/test(.*) $1 break;
}}
The "location /" - serving my HTML files and website perfectly.
But when I try to approach to "MyServersIP/test/" (serving a node app) which supposed to be served from "location /test" - the routing is good but NGINX serving it without any media.
On the chrome console I have inspected it in chrome and see the following errors:
GET http://MyServersIP/stylesheets/style.css net::ERR_ABORTED
GET http://MyServersIP/scripts/jquery.multiselect.js net::ERR_ABORTED
GET http://MyServersIP/css/jquery.multiselect.css net::ERR_ABORTED
I have tried to follow posts which I saw that people ran into the same problem:
Nginx fails to load css files ;
https://superuser.com/questions/923237/nginx-cannot-serve-css-files-because-of-mime-type-error ; https://www.digitalocean.com/community/questions/css-files-not-interpreted-by-the-client-s-browser-i-think-my-nginx-config-is-not-good
And many more, but nothing worked for me.
Another thing worth mentioning - when I swap routings like this:
server{
listen 80;
listen 443 ssl;
location / {
proxy_pass "http://127.0.0.1:5000";
}
location /test {
root /home/agent/lexicala;
rewrite ^/test(.*) $1 break;
}}
The node app is served perfectly, but it is not good for me as I want the users to approach my node app through the 'test' URL.
This is my nginx.conf file (I have made no changes):
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/*;
}
I tried to supply as much details as I could but if something is missing I would be glad to add.
Hope you guys help me find solution to this bug cause I spend over it good working days.

Nginx Not Showing Static Content on Localhost

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

why does nginx fail to start?

Yet starting nginx fails and my log shows:
[emerg] 55#55: "server" directive is not allowed here in /etc/nginx/nginx.conf:1
What am I doing wrong? Not sure if it matters but this is inside a docker container.
Try debugging first with
sudo nginx -t
If this passes but nginx restart fails, try checking
your nginx logs to narrow down the bug
/var/log/nginx/error.log
/etc/nginx/nginx.conf is not the place to store your server setups. This file should contain information on the user it will run under and other global settings. Typically it will contain stuff like this:
user www-data;
worker_processes 4;
pid /run/nginx.pid;
events {
worker_connections 768;
}
http {
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
server_tokens off;
client_max_body_size 500M;
include /etc/nginx/mime.types;
default_type application/octet-stream;
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/*;
}
Where you should put your individual server setups, is in /etc/nginx/sites-enabled/. As you can see, all files inside that folder are being included in the http block of your nginx.conf file.
So store the sample config you have posted inside /etc/nginx/sites-enabled/some-random-name.conf, and restore the nginx.conf file to the config I posted above, for instance, and you should be good to go.

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