Nginx trys to download file instead of displaying - nginx

I install Nginx and have a subdomain and domain. The subdomain has php5-fpm and wordpress. It works fine and is in one sites-available file symlinked to sites-enabled. The domain doesn't have php and has a file also symlinked. Even after restarting the server when I go to the domain it tries to download the html file. Here is my sites-available page for the domain:
server {
server_name www.example.us;
rewrite ^(.*) http://example.us$1 permanent;
}
server {
listen 80;
server_name example.us;
root /var/www/example;
index index.php;
autoindex on;
autoindex_exact_size off;
include /etc/nginx/security;
# Logging --
access_log /var/log/nginx/example.us.access.log;
error_log /var/log/nginx/example.us.error.log notice;
# serve static files directly
location ~* ^.+.(jpg|jpeg|gif|css|png|js|ico|html|xml|txt)$ {
access_log off;
expires max;
}
# location ~ \.php$ {
# try_files $uri =404;
# fastcgi_pass unix:/var/run/php5-fpm/example.us.socket;
# fastcgi_index index.php;
# include /etc/nginx/fastcgi_params;
# }
}
The nginx.conf file is:
user www-data;
worker_processes 4;
pid /var/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;
# Logging Settings
log_format gzip '$remote_addr - $remote_user [$time_local] '
'"$request" $status $bytes_sent '
'"$http_referer" "$http_user_agent" "$gzip_ratio"';
access_log /var/log/nginx/access.log gzip buffer=32k;
error_log /var/log/nginx/error.log notice;
# 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/x-javascript text/xml application/xml application/xml+rss text/javascript;
# Virtual Host Configs
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}

Remove default_type application/octet-stream;. This lines makes the browser think it's some binary data and not HTML.

As others noted, this is the line that causes your trouble:
default_type application/octet-stream;
This line overrides the line before it and tells Nginx to send any response as generic binary data. And, since browsers cannot do anything specific with binary data, they just download it as a file.
Removing or commenting that line, as well as the line above:
#include /etc/nginx/mime.types;
#default_type application/octet-stream;
should work, as default Nginx behaviour is to send everything as text/plain and browsers are smart enough to handle HTML/CSS/JS this way.
Despite that I wouldn't recommend such solution, as you may need to serve different types of content from your server, i.e. pictures, mp3's, binaries, etc.
Here's a better solution:
First, check that file /etc/nginx/mime.types; actually exists and accessible to read for the user that runs nginx.
Then, check that the file actually contains types { ... } declarations. That file comes packaged with Nginx and contains sensible defaults for mime-types to filename extensions mapping. If something is wrong with the file you can copy-paste its content from this gist:
https://gist.github.com/kondratovbr/dd34feba1d63bd468ded4ee70e59ea07
And finally, in nginx.conf change
default_type application/octet-stream;
to
default_type text/html;
Now Nginx will serve each type of content based on filename extensions. And if none provided, which is the case with some backend systems/apps, - will try to serve HTML.
Notice that you may want to change some of that defaults. For example, with that setting:
types {
audio/mpeg mp3;
}
browsers will usually try to playback the song rather than straight up download it. Which can be changed, if needed, by declaring:
types {
application/octet-stream mp3;
}

Also try clearing the cache for that site. I was having this problem in Firefox but rolling back to older configs wasn't working

Related

Nginx configuration is giving me 3 copy's of my domain in the address bar, how is this?

Overview
Okay, before I start, let me say I don't know much of Nginx and has routing works. What I know I learned in about 1 week's time. I'm more of an apache type guy. However, I'm working on a large scale project and would prefer using a faster server and not just apache server. So I decided on Nginx.
This issue relates to CSS/JS files not being resolved within the browser during page rendering for the frontend user.
I've spent over 3 days messing around and keep running into issues. I wouldn't be shocked if it's an easy fix though, so if you know Nginx and want to help me solve this issue please help me. I would, greatly.... greatly, appreciate a helping hand with this.
So I reckon that any dev's that want to help will need a copy of my Nginx config. It's probably also worth mentioning that I am using a hosting panel, called aaPanel on my Ubuntu 20.04 server, hosted in the cloud with IBM.
Other things to note:
aaPanel has two Nginx config files (I'm aware of), I'm messed around on each sub level Nginx config file. If I'm understanding this correctly, aaPanel uses a master Nginx config file and then a copy of the master conf file for your website. I fiddled with both, reverting changes, reloading Nginx etc, etc.
The frontend errors I have received via Opera Console or any other Browser's Console:
Failed to load resource: net::ERR_HTTP2_PROTOCOL_ERROR
Failed to load resource: net::ERR_CONNECTION_CLOSED
The main issue and some thinks I've identified
CSS/JS/IMG/Any MIME TYPE files do not load/resolve.
Any kind of mime type does NOT load, even if I directly visit them in the browser.
Nginx (I am 90% sure it is Nginx) is appending duplicates of my domain name for these files in the path. Seen below (Note that this is not the complete URL, but a small fraction of it since it's over 1mb long. Which leads me to believe it's a stackoverflow/endless loop issue.
https:// domain .net /domain.net/domain.net/domain.net/domain.net/filetype.exstension
(JS/IMG/CSS)
My two Nginx conf file configs I am aware of within aaPanel
My main website's NGINX CONFIG FILE:
server
{
listen 80;
listen 443 ssl http2;
server_name solidbets.net;
index index.php;
root /www/wwwroot/solidbets.net;
#SSL-START SSL related configuration, do NOT delete or modify the next line of commented-out 404 rules
#error_page 404/404.html;
ssl_certificate /www/server/panel/vhost/cert/domain.net/fullchain.pem;
ssl_certificate_key /www/server/panel/vhost/cert/domain.net/privkey.pem;
ssl_protocols TLSv1.1 TLSv1.2 TLSv1.3;
#Below ssl_ciphers changed for security PURPOSES
ssl_ciphers EECDH+CCHA20:EEH+CHACHA20-draft:EEH+AES128:RSA+AES128:EDH+A56:RSA+A6:+3DES:RSA+3DES:!MD5;
ssl_prefer_server_ciphers on;
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 10m;
add_header Strict-Transport-Security "max-age=31536000";
error_page 497 https://$host$request_uri;
#SSL-END
#ERROR-PAGE-START Error page configuration, allowed to be commented, deleted or modified
#error_page 404 /404.html;
#error_page 502 /502.html;
#ERROR-PAGE-END
#PHP-INFO-START PHP reference configuration, allowed to be commented, deleted or modified
include enable-php-74.conf;
#PHP-INFO-END
#REWRITE-START URL rewrite rule reference, any modification will invalidate the rewrite rules set by the panel
include /www/server/panel/vhost/rewrite/domain.net.conf;
#REWRITE-END
# Forbidden files or directories
location ~ ^/(\.user.ini|\.htaccess|\.git|\.svn|\.project|LICENSE|README.md)
{
return 404;
}
# Directory verification related settings for one-click application for SSL certificate
location ./* {
include /www/server/nginx/conf/mime.types;
}
access_log /www/wwwlogs/domain.net.log;
error_log /www/wwwlogs/domain.net.error.log;
}
My MASTER NGINX CONF File for the server:
user www www;
worker_processes auto;
error_log /www/wwwlogs/nginx_error.log crit;
pid /www/server/nginx/logs/nginx.pid;
worker_rlimit_nofile 51200;
stream {
log_format tcp_format '$time_local|$remote_addr|$protocol|$status|$bytes_sent|$bytes_received|$session_time|$upstream_addr|$upstream_bytes_sent|$upstream_bytes_received|$upstream_connect_time';
access_log /www/wwwlogs/tcp-access.log tcp_format;
error_log /www/wwwlogs/tcp-error.log;
include /www/server/panel/vhost/nginx/tcp/*.conf;
}
events
{
use epoll;
worker_connections 51200;
multi_accept on;
}
http
{
#AAPANEL_FASTCGI_CONF_BEGIN
fastcgi_cache_key "$scheme$request_method$host$request_uri";
fastcgi_cache_path /dev/shm/nginx-cache/wp levels=1:2 keys_zone=WORDPRESS:100m inactive=60m max_size=1g;
fastcgi_cache_use_stale error timeout invalid_header http_500;
fastcgi_ignore_headers Cache-Control Expires Set-Cookie;
#AAPANEL_FASTCGI_CONF_END
include mime.types;
#include luawaf.conf;
include proxy.conf;
default_type application/octet-stream;
server_names_hash_bucket_size 512;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
client_max_body_size 50m;
sendfile on;
tcp_nopush on;
keepalive_timeout 60;
tcp_nodelay on;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 64k;
fastcgi_buffers 4 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 256k;
fastcgi_intercept_errors on;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.1;
gzip_comp_level 2;
gzip_types text/plain application/javascript application/x-javascript text/javascript text/css application/xml;
gzip_vary on;
gzip_proxied expired no-cache no-store private auth;
gzip_disable "MSIE [1-6]\.";
limit_conn_zone $binary_remote_addr zone=perip:10m;
limit_conn_zone $server_name zone=perserver:10m;
server_tokens off;
access_log off;
server
{
listen 888;
server_name phpmyadmin;
index index.html index.htm index.php;
root /www/server/phpmyadmin;
location ~ /tmp/ {
return 403;
}
#error_page 404 /404.html;
include enable-php.conf;
location ~ /\.
{
deny all;
}
access_log /www/wwwlogs/access.log;
}
include /www/server/panel/vhost/nginx/*.conf;
}
URL REWRITE OPTIONS
If anyone has a solution to this very frustrating problem, which may be obvious, please help.

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 serving www.example.com but serves example.com

I am having problem getting www.example.com to work. Website loads on example.com but not on www.example.com. My current config file /etc/nginx/sites-available/example.com is
server {
listen 80;
server_name www.example.com;
return 301 http://example.com$request_uri;
}
server {
listen 80;
server_name example.com;
root /path/to/dir;
index index_en.html index.html;
}
I have tried different approaches with config, none worked. I also removed the example.com block and went with only one for www.example.com, yet www.example.com didn't work, but example.com still did. Of course I restarted nginx after every change.
I have DNS records for both, example.com and www.example.com, both A and pointing to same IP. I have also tried CNAME-ing one to another, waited for few hours and nothing. Still the same. Now I am getting really pissed off, because with other subdomains everything is working perfectly, just not with www.
And yeah, if I search for www.example.com on Firefox on Ubuntu 14.04, I get Server not found.
# /etc/nginx/nginx.conf
user www-data;
worker_processes 4;
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;
##
# 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/x-javascript text/xml application/xml application/xml+rss text/javascript;
##
# nginx-naxsi config
##
# Uncomment it if you installed nginx-naxsi
##
#include /etc/nginx/naxsi_core.rules;
##
# nginx-passenger config
##
# Uncomment it if you installed nginx-passenger
##
#passenger_root /usr;
#passenger_ruby /usr/bin/ruby;
##
# Virtual Host Configs
##
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
I left mail section out because it is completely commented out.
It probably had to do with some local DNS settings on LAN and local DNS cache, because I was able to access the site from other networks.

What to do with WordPress permalink on nginx?

I started a VPS yesterday and I installed WordPress and have done basic setup. But what is the deal with permalink? No one wish to use default ugly URL structure. I tried to understand http://wiki.nginx.org/WordPress but funny thing is "I don't know where to insert those codes". They probably think all of their readers are well known with nginx, that's not funny.
So, I am pretty sure .htaccess is not working in nginx. Now what? Where to place which code to make Custom permalink work? P.S: You know, if I set a custom permalink then the output is 404 page like:
So please, I want a simple guide that tell like "edit demo.php file and place below code after X".
ADDITIONAL INFO: My webroot is /usr/share/nginx/html, and I have two WordPress there. First: /usr/share/nginx/html directory
and second: /usr/share/nginx/html/video directory. I want pretty url for both of them. I chmod wp-content to 775 for both.
Nginx doesn't support .htaccess at all.
Is your nginx setup correctly so that php files are being processed by php-fpm? Your error screen shot is unclear about that.
You have to edit your nginx config file and tell it to use the wordpress controller to direct requests through.
Your nginx.conf should have an http block, and within that a server block, and within that some location blocks. You need to find the correct server block and insert the following location block
location / {
# Check if a file or directory index file exists, else route it to index.php.
try_files $uri $uri/ /index.php?$args;
}
Here is a complete example of a fully working wordpress nginx.conf I run:
user www-data;
worker_processes 1;
pid /run/nginx.pid;
events {
worker_connections 1024;
multi_accept on;
use epoll;
}
http {
client_max_body_size 20M;
fastcgi_buffers 16 16k;
fastcgi_buffer_size 32k;
sendfile on;
keepalive_timeout 30;
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;
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/x-javascript text/xml application/xml application/xml+rss text/javascript;
server {
listen 80 default_server;
server_name www.site.com;
root /var/www;
index index.php;
location / {
# Check if a file or directory index file exists, else route it to index.php.
try_files $uri $uri/ /index.php?$args;
}
# pass the PHP scripts to FastCGI server
location ~ \.php$ {
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
}

Resources