Nginx to serve contents of S3 files in browser - nginx

I want to open files from S3 served by Nginx in a browser. Was unable to get it working with following config
Files in S3 buckets are text files with extension .manefiest
location /manefiest/ {
proxy_pass http://my-bucket.s3-website-us-west-2.amazonaws.com/;
types {
text/html manefiest;
text/plain manefiest;
}
}
I want browser to show the contents of the file. But with above config it was downloading the file.
Whats wrong here ?

Setting contentType:"text/plain" for uploaded file solved the problem.
Thanks #ofirule

Related

nginx .jpg and .jpeg not found in / but are found everywhere else

I'm new to nginx and I like it.
I'm putting up a few scgi feeds, and some static content.
After creating a home page hierarchy under /home, I went to move it to /.
But when I put it there the .jpg and .jpeg images were coming back 404.
sitename.com/test.gif is found just fine.
sitename.com/test.jpg is 404
but creating a a subdirectory, and copying test.jpg work.
sitename.com/subdirectory/jpg is fine.
I'm running with pretty empty config files as they were installed under Ubuntu 18.
I'd REALLY like to know what's going on here, and why top level .jpg/.jpeg files are doing this.
I know I could create a location directive as follows:
location ~ \.(jpg|jpeg)$ {
root /real/location/of/my/home/;
}
But that breaks access to .jpg/jpeg files in the scgi locations served from other roots.
I found it! It wasn't the jpg/jpeg suffix. It was the filename!
I have a /wdc location that is an scgi script.
A file in / that begins wdc for example /wdc-face.jpg goes through to scgi.
Many of the nginx example give locations without trailing slashes. But I think that a trailing slash is what people should be encouraged to use by default

NGINX CSS Not Loading

I just set up my new vps to hold my website server. It works perfectly fine on my existing vps, but after I put it on the new one, none of the CSS files are loading.
Image here:
Any help is appreciated thanks!
In your server block in there has to be location for static files:
server {
server_name yourhost.com www.yourhost.com;
root /var/www/vhosts/yourhost.com/public/;
...
location /static {
alias /var/www/vhosts/yourhost.com/public/static;
}
}
So that
location /static {
alias /var/www/vhosts/yourhost.com/public/static;
}
has to be properly configured, pointing to the location where your static files are placed.
Then if your css is in file styles.css (or any other file) and is placed on the server as /var/www/vhosts/yourhost.com/public/static/css/styles.css, it will be accessible from /static/css/styles.css in your html.
To see where exactly this is configured, you can start with nginx.conf (normally in /etc/nginx/nginx.conf) and check what other conf files are imported.
Note the path provided is fully up to you, but once it's known it has to be properly configured with the alias in the static location.

How to serve file under root path using Nginx?

I have a website laike9m.com, which is served by Nginx. I want to access a text file AAA.txt with url laike9m.com/AAA.txt. The text file is on my server, let's say it's $HOME/AAA.txt. Redirection is not allowed. Current Nginx conf file is here. Thank you.
location ~ ^(.*\.txt)$ {
alias /home/laike9m/$1;
}
Solved it.

How to configure nginx to serve versioned css and javascript files

My css files named main.css?v=1.0.0 , site.css?=v.1.0 .
how to configure nginx to serve this kind of files(not extension with .css or .js but with a version number)
FYI:
all the files are in the right path and erro message in chrome dev tool console is file not found(404)
Thanks !
When you're trying to access main.css?v=1.0.0 or main.css?v=2.0.0 any webserver will point it to the same file, main.css
.
Well in your situation coud create a separate location for your versioned file, and then use the next code in the nginx config:
location = /main.css {
if ($arg_v) {
rewrite ([0-9]+) /where/maincss/versions/stored/main-$avg_v.css last;
}
// otherwise default main.css
}
The same thing'll be for the any other file

Nginx: Correctly setting the mime type for gzipped styles/scripts

I have a gzip-compressed style and scripts in files:
/scripts.js.gz
/styles.css.gz
The problem is that it does not serve the correct mime-type and browser does not recognize, that the files are compressed css or js (browser recognize the type as application/octet-stream, where it should be text/css or so).
I tried adding the following to mime.type of nginx, but with no effect. I suppose, it does not recognize, that the file is compressed.
types {
text/css css.gz;
application/x-javascript js.gz;
}
When trying to access the files, the browser handle the files as files to download and not to present.
I had the same problem, coming from Apache.
The problem I found is, the types block does not allow you to specify .css.gz as a file extension. However, the location block does! My solution is to make a location for .css.gz and then modify the content type for .gz within that location, like this:
location ~ \.css\.gz$ {
add_header Content-Encoding gzip;
gzip off;
types {
text/css gz;
}
}
Do not change the mime types and use the Gzip Static Module to handle this.
When this is active, Nginx will try to serve "file.ext.gz" first and then try "file.ext", if not found, for all requests within the context (location etc) where it is active.

Resources