I'm trying to upload a bunch of html and image files to my Nginx webserver which is running Ghost (the blogging platform) lets call it ghost-blog.com. Ghost runs perfectly fine, but additionaly I want serve other files and folders under the same domain e.g. ghost-blog.com/text.html and ghost-blog.com/subfolder/index.html.
After spending some time googling for an answer it seems I've bumped into something "new". I am aware I need to make changes to the /etc/nginx/sites-available/default file. What I don't know is what to add/edit so that
I create a /some/random/public folder public
This does not conflict with Ghost which is already serving content, specially the default index index.html index.htm files.
My current /etc/nginx/sites-available/default config file looks like this:
server {
listen 80;
server_name www.ghost-blog.com;
rewrite ^/(.*) http://ghost-blog.com/$1 permanent;
}
server {
root /usr/share/nginx/www;
index index.html index.htm;
server_name localhost;
location / {
try_files $uri $uri/ /index.html;
}
location /doc/ {
alias /usr/share/doc/;
autoindex on;
allow 127.0.0.1;
deny all;
}
}
Any suggestions on how I could go around creating a /public folder serving other files and sub-folders?
server {
listen 80;
server_name www.ghost-blog.com/subfolder;
rewrite ^/(.*) http://ghost-blog.com/subfolder/$1 permanent;
}
server {
root /usr/share/nginx/www/NEWSITEFOLDER;
index index.html index.htm;
location / {
try_files $uri $uri/ /index.html;
}
location /doc/ {
alias /usr/share/doc/;
autoindex on;
allow 127.0.0.1;
deny all;
}
}
then in ssh you need to make this directory to run the new ghost blog from
/usr/share/nginx/www/NEWSITEFOLDER;
so run command
mkdir /usr/share/nginx/www/NEWSITEFOLDER;
Related
I have an application with many urls like this:
dashboard.app.mycooldomain.com
subdomain-1.app.mycooldomain.com
subdomain-1.app.mycooldomain.com
subdomain-3.app.mycooldomain.com
subdomain-n.app.mycooldomain.com
and nginx config
server {
listen 5001 default;
listen [::]:5001;
server_name *.$hostname;
location / {
alias /usr/share/nginx/html/home/;
index index.html;
try_files $uri $uri/ index.html =404;
}
}
server {
listen 5001;
listen [::]:5001;
server_name dashboard.*$hostname;
location / {
alias /usr/share/nginx/html/dashboard/;
index index.html;
try_files $uri $uri/ index.html =404;
}
}
I expect when I visit dashboard.app.mycooldomain.com or dashboard.app.localhost nginx should serve all static file in /usr/share/nginx/html/home/ and when I subdomain-1.app.mycooldomain.com or subdomain-1.app.localhost or *.app.mycooldomain.com nginx should serve all static file in /usr/share/nginx/html/dashboard/. But now it does not work. How to write a config file correctly?
Create subdomains files in /etc/nginx/sites-enabled for all the required/different hosts/url, this way it would be much easier to manage the domains.
I have an IP address of my server that I want to put my website Frontend and Backend admin. The site1 part is simply should be at "http://IP/" and and site2 should be in "http://IP/admin" .
I have installed Nginx in server and my websites files are inside: Lets say its like :
site1: /var/www/html/site1/index.html
site2: /var/www/html/site2/index.html
I created 2 files in /etc/nginx/site-available/ called "site1.conf" and "site2.conf" .
site1.conf:
server {
listen 80;
listen [::]:80;
root /var/www/html/site1;
index index.html index.htm;
server_name http://myIP;
location / {
try_files $uri $uri/ =404;
}
}
site2.conf:
server {
listen 80;
listen [::]:80;
server_name http://myIP;
location /admin {
autoindex on;
alias /var/www/html/site2;
try_files $uri $uri/ /index.html last;
index index.html;
}
}
Then I linked these 2 files into "/etc/nginx/site-enabled"
After restarting the Nginx, my "http://ip/" opens site1 "index.html" and works fine.
but "http://ip/admin/" gives 404 error instead of opening site2 "index.html"
http://IP/ and http://IP/admin both point to the same server, with the server_name "IP".
Your server contains at least two location blocks.
For example:
server {
listen 80;
listen [::]:80;
server_name 1.2.3.4;
root /var/www/html/site1;
index index.html index.htm;
location / {
try_files $uri $uri/ =404;
}
location /admin {
alias /var/www/html/site2;
...
}
}
The server name only contains the text of the IP address or the DNS name. See this document for more.
You can spread your configuration across as many files as you choose. See the include directive.
The nginx configuration is a file called nginx.conf and contains an include statement to source all of the files in the sites-available directory. The content of these files are contained within the http { ... }.
As I have already stated, your two services are one server { ... } block, as far as nginx is concerned. However, you can still create a server block file in sites-available that includes files from some other location. Just don't use sites-avalable or conf.d, as nginx is aready using those directory names.
For example:
In sites-available/mysites.conf:
server {
listen 80;
listen [::]:80;
server_name 1.2.3.4;
include /path/to/my/location/confs/*.conf;
}
And in /path/to/my/location/confs/site1.conf:
root /var/www/html/site1;
index index.html index.htm;
location / {
try_files $uri $uri/ =404;
}
And in /path/to/my/location/confs/site2.conf:
location /admin {
alias /var/www/html/site2;
...
}
I am not saying that this is a good way to organise your files, but with nginx, many things are possible.
I have some static HTML/Javascript/CSS files that I'd like to serve at /.
But I also have a webserver that performs all of my API calls written in Python using Flask and uwsgi.
What I'm trying to do is to have all of my static content be accessible as localhost and my web API be accessible through localhost/api.
This is my default site in sites-enabled:
server {
listen 80;
server_name localhost;
root /var/www;
location /api {
location / {
try_files $uri #app;
}
location #app {
include uwsgi_params;
uwsgi_pass 127.0.0.1:3031;
}
}
}
As you can see I'm serving static content located at /var/www and I'm trying to make all requests to /api to be handled by uwsgi..
Currently when I try this, uwsgi gives me 404 and I think that it is because the uwsgi parameters aren't being passed.
From what I can gather of the documentation (http://flask.pocoo.org/docs/deploying/uwsgi/), the method you choose only works when the app is set to the URL root. I removed the try_files from your /api location as I do not believe it is needed since you are not serving static files from there. You may not need the rewrite either.
server {
listen 80;
server_name localhost;
root /var/www;
location / {
try_files $uri $uri/ =404
}
location = /api { rewrite ^ /api/; }
location /api {
include uwsgi_params;
uwsgi_param SCRIPT_NAME /api;
uwsgi_modifier1 30;
uwsgi_pass 127.0.0.1:3031;
}
This is my nginx configuration file:
server {
listen 80;
server_name localhost;
location / {
root d:/www;
index index.html index.htm;
}
location /js/api/ {
root D:/workspace/javascript/maplib/;
autoindex on;
}
}
And the directory of the document is like this:
D:/workspace/javascript/maplib
-- v1.0
--main.js
-- v1.1
Now I want to access the v1.0/main.js by http://localhost/js/api/v1.0/main.js.
And it returns a 404 error.
It seems that ngnix will tried to get the file through D:/workspace/javascript/maplib/js/api/v1.0/main.js which does not exist.
It seems that the string path in the location(in the url) must exist at the file system.
How to fix it to meet my requirement?
BTW, there is not only the js but also some other kinds of files like .gif,.png,.html inside the D:/workspace/javascript/maplib/.
Use alias. Ref: http://nginx.org/en/docs/http/ngx_http_core_module.html#alias
That is, replace
root D:/workspace/javascript/maplib/;
by
alias D:/workspace/javascript/maplib/;
Use rewrite inside location /js/api/ for this, like:
rewrite ^/js/api(.*)$ $1;
You can use root with try_files, just add the try_files line
location /js/api/ {
root D:/workspace/javascript/maplib/;
autoindex on;
try_files $uri $uri/ =404;
}
I'm looking to serve the root url of a subdomain and directory of a subdomain to two different folders on my server. Here is the simple set-up that I have and is not working...
server {
index index.html index.htm;
server_name test.example.com;
location / {
root /web/test.example.com/www;
}
location /static {
root /web/test.example.com/static;
}
}
In this example going to test.example.com/ would bring the index file in /web/test.example.com/www
and going to test.example.com/static would bring the index file in /web/test.example.com/static
You need to use the alias directive for location /static:
server {
index index.html;
server_name test.example.com;
root /web/test.example.com/www;
location /static/ {
alias /web/test.example.com/static/;
}
}
The nginx wiki explains the difference between root and alias better than I can:
Note that it may look similar to the root directive at first sight, but the document root doesn't change, just the file system path used for the request. The location part of the request is dropped in the request Nginx issues.
Note that root and alias handle trailing slashes differently.
The Location directive system is
Like you want to forward all request which start /static and your data present in /var/www/static
So a simple method is separated last folder from full path , that means
Full path : /var/www/static
Last Path : /static and First path : /var/www
location <lastPath> {
root <FirstPath>;
}
So lets see what you did mistake and what is your solutions
Your Mistake :
location /static {
root /web/test.example.com/static;
}
Your Solutions :
location /static {
root /web/test.example.com;
}
server {
index index.html index.htm;
server_name test.example.com;
location / {
root /web/test.example.com/www;
}
location /static {
root /web/test.example.com;
}
}
https://nginx.org/en/docs/http/ngx_http_core_module.html#root
A little more elaborate example.
Setup: You have a website at example.com and you have a web app at example.com/webapp
...
server {
listen 443 ssl;
server_name example.com;
root /usr/share/nginx/html/website_dir;
index index.html index.htm;
try_files $uri $uri/ /index.html;
location /webapp/ {
alias /usr/share/nginx/html/webapp_dir/;
index index.html index.htm;
try_files $uri $uri/ /webapp/index.html;
}
}
...
I've named webapp_dir and website_dir on purpose. If you have matching names and folders you can use the root directive.
This setup works and is tested with Docker.
NB!!! Be careful with the slashes. Put them exactly as in the example.
If you use this, I will suggest you set up this command too.
location /static/ {
proxy_set_header Host $host/static; // if you change the directory and the browser can't find your path
alias /web/test.example.com/static/;
}
If you want to check two different directories for the same URI use this config:
server {
...
root /var/www/my-site/public/;
...
index index.php index.html index.htm;
...
location / {
root /var/www/old-site/dist/;
try_files $uri $uri/ /index.php$is_args$args;
}
...
}
If Nginx couldn't find file in /var/www/old-site/dist/ directory, then it will try file in /var/www/my-site/public/ directory, but as we said to Nginx to try files with $uri $uri/ /index.php$is_args$args patterns, so Nginx will try /index.php$is_args$args in /var/www/my-site/public/ directory. not $uri
If you want to complete your fallthrough, then replace /index.php$is_args$args with /fallthrough$uri and then add the location /fallthrough { ... } with the alias key to your target directory.
https://www.nginx.com/resources/wiki/start/topics/tutorials/config_pitfalls/#root-inside-location-block