nginx and django - gettng cache-control to work - django

I am trying to get cache-comtrol to work.
In nginx I have the following:
location /static/ {
alias /home/ubuntu/workspace/mysite;
expires max;
add_header Cache-Control public;
}
in mysite dir I have static. In static I have the dirs for js, css, and images.
In the web browser I get 404 error. Images cant be found. If I remove location /static/ this site works but I have no cache-control.
How do I resolve?

If you use url like: http://your.page.com/static/image.gif then with your rules you get such uri (including alias directive):
/home/ubuntu/workspace/mysiteimage.gif;
So conclusion is that remove last / from location directive (it should be /static) or add at the end to alias directive / (so it will be as alias /home/ubuntu/workspace/mysite/;)
Other solution could be like:
location ~ (static/.*) {
alias /home/ubuntu/workspace/mysite/$1;
# ... rest of code here
}
Then you don't have to add static again in alias directive. You just use it as location param ($1 = static/.*).
Why that way? alias directive (alias doc) work as follow: it trims from requested url part matched in location directive end then, with what will stay, append to it own rule path. In your example from url /static/image.gif/ it will trim /static/ and to your alias path append only image.gif so it will look like I wrote: /home/ubuntu/workspace/mysiteimage.gif/`.

Related

NGINX 301 remove .html from url, but not for files from a specific path (folder)

In my location / directive I have added the following if statement to redirect all requests with the .html extension to the non-extension url:
if ($request_uri ~ ^/(.*)\.html) {
return 301 /$1$is_args$args;
}
This works fine, but actual .html files are also being redirected which results in a 404. I want to avoid this by adding an exception for all requests if the the url contains the directory "/static/", but I am not sure how to do that. I tried to add the following if statement before the if statement above:
if ($request_uri ~ /static/) {
}
But I'm not sure what to put in it to stop it from executing the .html if statement. Return $host$request_uri doesn't work, unfortunately.
How would I be able to do this? I cannot use location directives due to limitations of my host. Everything is being done in in the location / directive.

Get url parameter and save variable or cookie

I have a new installation of wordpress that replaces an old site.
In the old site there was a dynamic referl for users
mysite.com/123456 or mysite.com/somename
Now I have to be able to intercept everything that exists after / then 123456 or somename to save it in a session variable or in a cookie, (I have full access to the server).
I did some test with this code:
location / {
add_header Set-Cookie "secret_code=$args;Domain=$site_name;Path=/;Max-Age=31536000;Secure;HTTPOnly" always;
try_files $ uri $ uri / /index.php?$args;
}
But I find myself as a value other parameters, very likely for calls that makes wordpress itself
Also with this:
location ~ ^/(.+)$ {
add_header Set-Cookie "secret_code=$1;Domain=$site_name;Path=/;Max-Age=31536000;Secure;HTTPOnly" always;
try_files $ uri $ uri / /index.php?$args;
}
But this does not work nginx because it does not run PHP, and it makes them download
Which is the best way to solve this problem
Thank you guys
I'm not sure if this is all you wanted. But, here is the regex to match alphanumeric characters after '/'
Tested and works for both mysite.com/123456 and mysite.com/somename
/(?<=mysite.com\/)\w+/

Nginx rewriting Rules

I have the following url:
mywebsite.com/template/product.html
and I want to rewrite it as
mywebsite.com/product
location / {
alias /var/www/web/;
rewrite ^/(mywebsite.com/template/.*)\.html$ /$1 last;
}
Not sure if you want to handle the domain as a generic variabile.
Anyway, if you setup a server conf for mywebsite.com, this configuration should fit your case.
server {
error_log logs/mywebsite.com.log debug;
server_name mywebsite.com;
root /var/www/web/mywebsite.com;
# this directive handle the redirects from old name.html to new format
location /template/ {
rewrite ^/template/([^.]+)\.html$ /$1 redirect;
}
# given new new url format this directive match the name and
# read the file
location ~* ^/([^.]+)$ {
try_files /template/$1.html /template/notfound.html;
}
}
The regular expression you wrote did not match the product name. Now the group should fit your requirements. I have also modified the rewrite flag last in redirect because I suppose you want redirect a browser or a bot to the new urls.

Can NGINX change the response code after a proxy_pass?

So I have an internal API server namespaced under /api/, and I want to pass all other requests to an Amazon S3 static site using proxy_pass. This all works fine, it's just since Amazon is serving a single page app, I want to always return the same HTML file. They way I did this with the S3 server, was to set the index and error page as the same file. It all looks fine on the surface, but for all other requests besides /, the S3 instance returns a 404. Can I use NGINX to change this to a 200 before returning it to the client?
server {
listen 80;
server_name example.com;
location /api/ {
# serve internal app
}
location / {
proxy_pass http://example.amazonaws.com/;
# ALWAYS RETURN A 200
}
}
You should be able to use the error_page and proxy_intercept_errors directives to achieve this. Something like this should do the trick.
location / {
proxy_pass http://example.amazonaws.com/;
proxy_intercept_errors on;
error_page 404 =302 /your_html_file
}
error_page
proxy_intercept_errors
You can internally rewrite all URLs to the document you want served. This avoids the error handling cycle and problematic redirects.
It would be something like (untested):
location / {
proxy_pass http://example.amazonaws.com/;
rewrite ^.* /index.html
}
Note that you will want to only use full or root-relative URLs in your doc, because you don't know if the docs is served from a subdirectory.
You'd also be wise to have JS code validate the URL and optionally redirect to one you consider valid. Otherwise 3rd party sites could link to offensive URLs and get them in search indexes!

Nginx exactly ROOT directive (not anything)

I have a RESTful server. I serve my static files with Nginx. I want the root url ( www.website.com, www.website.com/ ) to point to a static html file, and EVERY other request to be redirected to my RESTful server. What I have:
location / {
proxy_pass http://localhost:5000/;
}
location /static {
autoindex on;
alias "some location";
}
location /media {
autoindex on;
alias "some location";
}
What I need: a directive that redirects only the Root URL (not everything like /).
Nginx location directive has = modifier for that.
location = / {
# only request to '/' gets here
}
location / {
# all other goes here
}