Nginx: how to let rewrite rules ignore files or folders - html

I use Nginx to serve a SPA (Single Page Application), in order to support HTML5 History API I have to rewrite all deeper routes back to the /index.html, so I follow this article and it works! This is what I put in nginx.conf now:
server {
listen 80 default;
server_name my.domain.com;
root /path/to/app/root;
rewrite ^(.+)$ /index.html last;
}
However there's one problem, I have an /assets directory under the root contains all the css, js, images, fonts stuffs, I don't want to rewrite these urls, I just want to ignore these assets, how am I suppose to do?

Put rewrite into one location and use other locations for assests/dynamic urls/etc.
server {
listen 80 default;
server_name my.domain.com;
root /path/to/app/root;
location / {
rewrite ^ /index.html break;
}
location /assets/ {
# Do nothing. nginx will serve files as usual.
}
}

Related

How can I remove /public and the name from URLs in Bolt?

I'm working on a small site and I've got a question.
How can I remove the /public and the singular name from url? However for the entries I want to keep the first segment.
How do I do that? Because when I try to rewrite it in the htaccess the assets break (images etc)
You need to point your server to the /public directory. Here is what the official documentation is saying:
Only the 'public' folder needs to be accessible in the browser. After the first installation this folder is named public/ but as you read on, you will see that you can rename it to www/ or whatever your web server requires. To do this, configure your webserver to use the public/ folder as the web root. For more information about this, see the pages on configuring Apache or Nginx.
Source: https://docs.bolt.cm/3.2/installation/install-command-line
The directive for Apaches VirtualHost is (in your example domaincom.conf in /etc/apache/sites-enabled:
<VirtualHost *:80>
DocumentRoot /your-bolt-directory/public
</VirtualHost>
And for Nginx:
location / {
root /your-bolt-directory/public;
}
Use .htaccess file to rewrite your URL's:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(.*)$ public/page [L]
</IfModule>
The public part of the url should not be visible if you have your Apache virtualhost setup correctly.
Ordinarily, all you need to do here is point the DocumentRoot setting to the public directory, so for example if your setup currently looks like this:
DocumentRoot /home/mysite
You adjust it to:
DocumentRoot /home/mysite/public
You may also need to do the same for any <Directory /home/mysite > commands that are in your config too.
Once you have amended, restart Apache and http://yourdomain.com/ should load up the homepage and http://yourdomain.com/bolt take you to the backend.

How to serve any static directory containing web pages in django?

I'm working on an application where we want the following to happen:
The administrators upload zip archives containing web content (whole static websites) through admin interface
The archive is unzipped in the background to a random directory
The static content is served at a url that looks like http://<host>/site/<user-friendly-url>/, with the index.html at the root, all the paths to css and js files, etc.
I know how to configure the route, but I was wondering what to put in the view. I have tried with STATIC_{ROOT,URL} and MEDIA_{ROOT,URL} but I feel it's not the right way.
In summary : is there a way to put something like return serve_this_static_directory() in the view? or is there any workaround there?
Thanks for your answers
edit:
Trying to clarify my question.
The url (http://<host>/site/<user-friendly-url>/) doesn't match the directory name (which could be anything, like XYZ_html/).
Administrators must be able to upload new archives, which should be extracted and served automatically.
For those two reasons, I don't have any idea how to serve this content using the webserver, because how is it possible to configure the routes then?
First you must realize that django won't serve your static or media files by default, you should use your HTTP server (nginx, apache or other) for that.
Next thing to do is to configure your HTTP server to serve "/path/to/your/project/public" and any files inside on root URL of your domain, and instead of serving 404 error if file is not found, it should redirect to django (so if there is some file in path /site/something/ your HTTP server should serve that file, if not, it shoudl serve anything that django will output on that path. Sample configuration for nginx might look like this:
server {
listen 80;
server_name oskar.local;
root /path/to/your/project/public;
location #default {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
include /etc/nginx/uwsgi_params;
uwsgi_pass app_server; # or some proxy_pass if you're not using uwsgi
break;
}
location / {
try_files $uri #default;
}
}
And last step is to configure your django app's MEDIA_* settings:
MEDIA_ROOT = "/path/to/your/project/public"
MEDIA_URL = "/"
But be careful! With that approach there is potential vulnerability: any user can put files into path that is normally handled by django. Better approach will be to put all of "sub-sites" inside some sub-directory or into separate domain.

nginx only serving welcome index page

I'm running nginx off of EC2 server
to try to troubleshoot this I created a separate directory usr/loca/nginx/html/test and put a copy of the index.html file there. I deleted the original index.html file from usr/loca/nginx/html
here is my relevant code...
nginx.conf.default
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
include usr/local/nginx/conf/*.conf
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
location / {
root html/test;
index index.html index.htm;
}
}
i try to access the site by http://'public ip address'/index.html and all i get is the default nginx startup page; even though i deleted this file, and even though i changed the text in the original index.html file to something else
i tried reloading nginx and clearing cache etc.....
I think your root directive is not pointing to the correct location. Your root directive is relative i.e. relative to the current folder. This is a very bad thing to do. Always do root relative directive. I will do something like
/home/user/www/localhost
You can choose a directory of you liking but make it root relative. Note the leading slash. If welcome page is working then NGINX is working correctly and the fault usually lies in wrong root directive. You can also make the following improvements.
First of all get the root out of sever block. Putting root in the server block is bad.
http://nginx.org/en/docs/beginners_guide.html#proxy
Why is your Nginx configuration file called nginx.conf.default? It should just be called nginx.conf (this may also be leading to the problem). See if you have a file called nginx.conf and make changes to that.
Why are you declaring the server inside nginx.conf? It is better to declare it inside a folder like
sites-available/localhost
sites-available/somesite
...
Then create a symlink to sites-enabled. This is not required but it is a good practice.

Nginx css and js 404

I have the below in my nginx file for a domain:
location / {
root /home/dpk/apps/abc/site;
index index.html;
}
When I access the domain, only the text is visible, all static files like favicon.ico, css and js files are giving a 404 error. As a result the layout is completely broken.
css is located in /home/dpk/apps/abc/site/assets/home.css and similarly js is stored in /home/dpk/apps/abc/site/assets/home.js.
The root directory is recursively owned by www-data and all files are 755. This is a pure html website and no other language like php is being used.
I guess I am missing something in the configuration. Any suggestions?
I got it working by using just root /home/dpk/apps/abc/site; instead of the below.
location / {
root /home/dpk/apps/abc/site;
index index.html;
}

How to use root-relative URL's on Apache server without a domain name?

I have a staging server (VPS) that has a dedicated IP address. For example, http://numeric.ip.address/
I can access the the files in the public_html folder by doing the usual tilda thing in the URL - http://numeric.ip.address/~account/
I have a folder I've developed locally that uses root-relative paths to resources, but
when I load the file in the browser, those root-relative URL's jump up in the directory all the way to the root IP address, instead of the public_html folder /~account/.
I realize there are better ways to set up a staging server and I plan to do so in the future, but I'm facing a deadline where it would be really handy to make this work.
I've tried tossing a base href tag in header tags, but that isn't doing the trick.
In Apache, normally you have "VirtualHost" container or .htaccess file. In it, you set your domain and DocumentRoot.
From what you describe, all you need to do is set your DocumenRoot correctly to you root of your site. then in the Browser, you can point to your paths. So if you have folder called "finance" and inside that folder you have document call "report1.pdf", you can put in your browser, example.com/finance/report1.pdf -as long as you have your DocumentRoot and site setup correctly and put the files in correct path, Apache will serve the file.
<VirtualHost 10.1.2.3>
ServerName example.com
ServerAlias www.example.com # not necessary
DocumentRoot /www/docs/host.foo.com
...omitted the <Direcotry> </Directory> stuff
</VirtualHost>