My contact.html file is being downloaded rather than rendered in the browser when running my site on NGINX.home.html is working properly. This is how my default (in folder sites-available) file looks like:
server {
listen 90;
listen [::]:90;
server_name example.com;
root /home/myname/www;
location / {
try_files $uri /home.html;
add_header Access-Control-Allow-Origin *;
}
location = /contact {
default_type text/html;
alias /home/myname/www/contact.html;
}
}
When I add /contact to my url on my browser, contact.html gets downloaded as unknown file format. After having done an extensive search, these are the things I've tried:
Clear the browser cache (it also happens in Edge, so clearly this isn't the issue)
In nginx.conf I commented out the default_type application/octet-stream and un-commented default_type text/html
I have checked the in mime.types file the type text/html exists.
using try_files $uri /contact.html
Any help will be appreciated!
The issue was that default_type text/html property in nginx.conf lives in http {...} block. Since my server is listening to port 90 this configuration does not apply. once I changed the port to 80 the issue was resolved.
Related
I have the following nginx.conf:
events {}
http {
server {
index index.json;
location /api/some-folder {
alias /some-folder;
default_type application/json;
add_header Content-Type application/json;
}
}
}
With that in place, when the nginx docker image is served on port 8080 and has my json files packed in folder /some-folder, I can browse
http://localhost:8080/api/some-folder/
and I get the content of /some-folder/index.json back. Furthermore, when I browse
http://localhost:8080/api/some-folder/subfolder_1/subfolder_2/some-json.json
then I get the content of /some-folder/subfolder_1/subfolder_2/some-json.json. All fine.
My issue is that I can't seem to find a way to get the /some-folder/index.json content when I browse
http://localhost:8080/api/some-folder
with no trailing slash. When I browse that, I get redirected to
http://localhost/api/some-folder/
which doesn't exist, because nothing is served on port 80. I think I am missing something on how nginx works, and I'd appreciate some help to make it work.
If I simplify my location to just /, then the following configuration works perfectly with and without trailing slash:
events {}
http {
server {
index index.json;
location / {
root /some-folder;
default_type application/json;
add_header Content-Type application/json;
}
}
}
I would like the same behavior as that, but with a location /api/some-folder.
EDIT
Please find here a github repository where the issue is reproduced.
I have an nginx server that is showing a 200 response in my browser for requests like
https://server.com/app/static/js/2.8cc049f3.chunk.js
and showing a 304 on the server logs
"GET /static/js/2.8cc049f3.chunk.js HTTP/1.1" 304 0 "https://server.com/app"
There is another nginx in front of the nginx running on server.com that is removing the app from the path. Based on the nginx logs on server.com this is working correctly. The static folder is in my root /usr/share/nginx/html/.
The content my browser receives for the js file is the index.html. However when I login to the server and run
curl http://localhost/static/js/2.8cc049f3.chunk.js
I get the correct js content in response and in the logs the server prints 200
"GET /static/js/2.8cc049f3.chunk.js HTTP/1.1" 200 1570391 "-" "curl/7.80.0" "-"
Here is my nginx.conf
server {
listen 80;
root /usr/share/nginx/html/;
index index.html;
add_header X-Frame-Options "SAMEORIGIN";
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
location / {
try_files $uri $uri/ /index.html;
}
location ~ ^/(static)/ {
gzip_static on;
gzip_types
text/plain
text/xml
text/css
text/comma-separated-values
text/javascript application/x-javascript
application/atom+xml;
expires max;
}
}
I've read that a 304 means the file hasn't changed and tells your browser to use its local cache, so I cleared my browser cache. I also restarted nginx, thinking that the first request would give a 200 response on the server but it was still a 304.
Based on the local curl request being successful I don't think there is anything wrong with my nginx.conf. I don't know if nginx somehow has the index.html cached as the content of my js, or if I didn't clear my browser cache correctly.
I'm also confused why the response code is 304 on the server but 200 in my browser.
The HTTP 304 says "Not-Modified". This is because you are using another NGINX Proxy server in front of the NGINX Server we are talking about.
The 1st NGINX is requesting a resource on the 2nd NGINX and this one answers "Hey that file was not modified since the last time you have asked".
In this case it would be very helpful to check the configuration of the 1st NGINX Proxy instance or your turn of the caching in the first one and proxy_cache off; and check the result.
I'm trying to set up an nginx server to serve a React app at the address http://mydomain/memorygame
Currently I have the following nginx routing config:
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/mydomain.com;
index index.html;
server_name mydomain.com www.mydomain.com;
location /memorygame {
root /var/www/mydomain.com/memorygame;
try_files $uri /$uri $uri/ /index.html $uri/index.html =404;
location ~* \.(css|js)$ {
try_files $uri /$uri =404;
}
}
The css files are stored under /var/www/mydomain.com/memorygame, in the index.html the link is /static/css/main.d5dd0bd5.css.
The index.html load fine, but the css requests aren't routed to where I want them. If I take out "/index.html" from try_files, the css loads, but index.html doesn't. How can I make them both work at the same time?
(My guess is that part of the problem is that css request issued goes to mydomain.com/static/css/style.css instead of mydomain.com/memorygame/static/css/style.css, but I might be wrong. If I'm correct, is there a way to auto-prepend the /static/css/style.css to be relative to the mydomain.com/memorygame folder?)
Thanks in advance!
I figured it out myself. Deleting everything out of the location /memorygame block did the trick. Nginx does what I was trying to achieve by default.
Hey there,
I'm new when dealing with NIGNX servers and Linux. My HTML file is displayed but my server does not load the CSS files.
The only thing I found was this line
include /etc/nginx/mime.types;
which I include in the http block.
After that I reload my config with sudo nginx -s reload. To be sure I also executed sudo nginx -s stop and sudo nginx.
This is my whole config:
http {
include /etc/nginx/mime.types;
server {
location / {
root /data/www;
}
location ~ \.(gif|jpg|png)$ {
root /data/www/images;
}
}
}
events {}
My skeleton files are located in /data/www. In this directory there is another CSS folder.
Thank you in advance.
First of all, you're going to need to tell NGINX to have your static files to obtain a TTL (time to live) via expire headers. Locate this in your NGINX configuration file, if it isn't there. Create a new directive with location
location ~* .(jpg|jpeg|png|gif|ico|css|js)$ {
expires 1s;
}
After this go ahead and purge your files from the server and force it to serve new files.
Set sendfile off in nginx.conf
Set expires 1s in mysite.conf
Explicitly set Cache-Control header: add_header Cache-Control no-cache;
Of course, before doing anything above. If it doesn't require drastic measure, try manually deleting everything in the cache folder: /var/cache/nginx
If that doesn't help then proceed with everything listed here!
After you've successfully purged your server from serving static files. Add this to your NGINX server block to achieve optimization.
gzip on;
gzip_comp_level 2;
gzip_min_length 1000;
gzip_proxied expired no-cache no-store private auth;
gzip_types text/plain application/x-javascript text/xml text/css application/xml;
It's possible to set expire headers for files that don't change and are served regularly.
location ~* .(jpg|jpeg|png|gif|ico|css|js)$ {
expires 365d;
}
So I've been trying to get HLS working over HTTPS. This would seem like a simple task but I've hit a roadblock.
I can get HLS streaming over HTTP with no issues as its really straight forward. However as soon as I change over to HTTPS non of my clients can seem to play it. Most posts that I've researched want to talk about encrypting the HLS content, but i don't really care. I just want to serve it.
What I've also noticed is that the .m3u8 is getting downloaded by the client, but my guess is that the chunks aren't, which is why the stream errors. Also the chrome tools for debugging done show any errors on the video object.
Here is my nginx configuration:
#
# HTTP server
#
server {
listen 80;
server_name localhost;
root /var/www/html;
index index.html index.htm;
location /hls/ {
types {
application/vnd.apple.mpegurl m3u8;
video/mp2t ts;
}
add_header Cache-Control no-cache;
try_files $uri $uri/ =404;
}
}
#
# HTTPS server
#
server {
listen 443;
server_name localhost;
root /var/www/html;
index index.html index.htm;
ssl on;
ssl_certificate /etc/nginx/ssl/lab.company.com.crt;
ssl_certificate_key /etc/nginx/ssl/lab.company.com.key;
ssl_session_timeout 5m;
ssl_protocols SSLv3 TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers "HIGH:!aNULL:!MD5 or HIGH:!aNULL:!MD5:!3DES";
ssl_prefer_server_ciphers on;
location /hls/ {
types {
application/vnd.apple.mpegurl m3u8;
video/mp2t ts;
}
add_header Cache-Control no-cache;
try_files $uri $uri/ =404;
}
}
This was a configuration issue. You need to make sure you are not gzipping, and that the security certificate is valid.