Nginx Config Issue for Ghost Blog inside a sub-directory on a LEMP Server
My Requirements
Ghost Blog on www.mydomain.com/blog
An index.html, contact.php, couple of images and js files in root directory. (i.e something like www.mydomain.com/index.html, mydomain.com/contact.php, etc)
After Reading This Tutorial https://www.digitalocean.com/community/tutorials/how-to-create-a-blog-with-ghost-and-nginx-on-ubuntu-14-04 , I m able to run ghost on www.mydomain.com/blog. But Unfortunately, I Cant Access anything from root directory i.e. www.mydomain.com as it throws a 404 Error
Prior to Ghost Install Everything inside /usr/share/nginx/www
was available on root directory. Now, I used ls
command to check whether the files are present inside folder /usr/share/nginx/www
and I can View these files on my terminal.
When I cd
to /etc/nginx/sites-available/
folder, I can see 2 files ghost and default and inside /etc/nginx/sites-enabled/
I can see only ghost file.
When I copy the default file from /etc/nginx/sites-available/
to /etc/nginx/sites-enabled/
, Ghost Blog gives a 404 Error.
Here is The default file Content server { listen 80 default_server; listen [::]:80 default_server ipv6only=on;
root /usr/share/nginx/html;
index index.php index.html index.htm;
# Make site accessible from http://localhost/
server_name localhost;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
# Uncomment to enable naxsi on this location
# include /etc/nginx/naxsi.rules
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
Here is The ghost file Content
server {
listen 80;
server_name SERVERIP/spider;
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $http_host;
proxy_pass http://127.0.0.1:2368;
}
}
I found The Solution after some Intensive Google Search
Now, First I Copied The Default Ghost Config file from sites available folder to sites enabled folder using
cp /etc/nginx/sites-available/default /etc/nginx/sites-enabled/default
After The file Got copied,I Restarted nginx using following command
service nginx restart
Now, When I refreshed The page, I couldn't see my blog but everything from the folder /usr/share/nginx/www
being available on root directory. i.e all files from that folder were accessible on mydomain.com
Coming back to the Ghost Blog now, I made a backup of the same and removed it from the /etc/nginx/sites-enabled/default
folder.
Instead, I pasted Following Code inside server object.
location ^~ /blog {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://127.0.0.1:2368;
proxy_redirect off;
}
Restarted nginx, it worked!
Source https://allaboutghost.com/how-to-install-ghost-in-a-subdirectory/