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.
Related
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"
I'm trying to create a basic api which does stuff, as an api does, however it is sitting behind both an Nginx instance and a Cloudflare layer for security, however every time I make a request all the headers go through find but the body of the request (application/json) seems to be getting removed.
I have tried logging it on the nginx instance and I just get '-' every request so I think it could be Cloudflare. I have tested locally and I am definitely able to receive the body as it is. I've looked through the req object and there is no body anywhere, all the auth headers are fine just the body.
EDIT (in response to AD7six): Sorry i'll clear my question up, i'm saying that both the access log is missing the body and that my code behind the proxy does not receive it. I'll attach the nginx config / log now.
On further inspection my nginx config is listening to port 80 however all the responses are going to https... I hope that makes sense.
NGINX Config
user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;
events {
worker_connections 768;
# multi_accept on;
}
http {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
log_format postdata $request_body;
##
# 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/*;
server {
listen 80;
listen [::]:80;
server_name dev.ru-pirie.com;
location / {
access_log /var/log/nginx/postdata.log postdata;
proxy_pass http://192.168.1.74:3000;
}
}
}
All the log contains is - per request
When requests are proxied via Cloudflare, by default they are modified with additional headers, for example CF-Connecting-IP that shows the IP of the original client that has sent the request (full list here).
There are other features that Cloudflare users can implement that may alter the request, but only when explicitly configured to do so: for example, someone could write a Cloudflare Worker that modifies arbitrarily the incoming request before forwarding it to the origin server. Other general HTTP request changes are possible using Cloudflare Rules.
Cloudflare would not alter the body of an incoming request before passing it to the origin, unless explicitly configured to do so for example with Workers.
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!
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.
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