How to configure nginx to serve versioned css and javascript files - nginx

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

Related

Nginx to serve contents of S3 files in browser

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

Expose files to public via rewrite

I'm using nginx docker engine for a symfony app, and I have some images under /var/www/html/project/ezplatform/files/(.*) that i need to expose them to the public.
I tried to add under my nginx configuration this rewrite config:
rewrite "^/var/www/html/project/ezplatform/files/(.*)" "/app.php" break;
rewrite "^/var/www/html/project/ezplatform/files/(.*)" "/var/www/html/project
/ezplatform/files/$1" break;
But still facing an exception 404 Not Found when I try to access to an available image.
Any idea would be appreciated.
Try adding a location block to your nginx conf, where you define a handle for the images directory, which nginx then uses to retrieve files from the full path. Like:
location /other_images/ {
alias /var/www/html/project/ezplatform/files/;
}
You can link to the images using your 'virtual' folder path:
other_images/example.gif
(So in HTML, <img src = "other_images/example.gif">, or use whatever the symfony syntax is for image links.)
Let me know if this solves the 404 Not Found problem.

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.

The default CSS of Django admin section is not loading

Folks, the default CSS of my Django admin section is not loading (setup uses nginx reverse proxy + gunicorn, OS is the Debian-based Ubuntu).
The following is part of etc/nginx/sites-available/myproject:
location /static/admin {
alias /home/mhb11/.virtualenvs/myenv/local/lib/python2.7/site-packages/django/contrib/admin/static/;
}
That, btw, points to the correct location of django admin's css files, and is written below location /static/ {} snippet (not shown here).
Note that I have tried the root directive instead of alias too, to no avail. Also note that this error pertains solely to django admin static files. The project related static files are working perfectly. Also note that my settings.py file includes 'django.contrib.staticfiles', in INSTALLED_APPS and STATIC_URL = '/static/'.
What am I missing? Please ask for more information in case it is needed.
It may not be significant, but for consistency, your location path and alias path should both end with a / or neither end with a /.
With your current configuration, the server is constructing path names with an embedded //, like /home/mhb11/.virtualenvs/myenv/local/lib/python2.7/site-packages/django/contrib/admin/static//somefile.css.
Try:
location /static/admin/ {
alias /home/mhb11/.virtualenvs/myenv/local/lib/python2.7/site-packages/django/contrib/admin/static/;
}

In nginx config, how to show something to log file directly in the config file?

I'm config a nginx, and debugging the config file,
How to show something from the config file directly to log file?
for example:
location ..... {
to_log "some string";
}
There is no direct way (on the todo list of the echo nginx module), but this solution seems fine https://serverfault.com/questions/404626/how-to-output-variable-in-nginx-log-for-debugging

Resources