Ghost and Nginx Reverse Proxy Question

I would like to be able to create a subdirectory manually through SFTP and upload custom HTML to the subdirectory that is separate from Ghost. Upon researching this, I see that a reverse proxy needs to be configured in Nginx. My hosting is on a DigitalOcean droplet. I actually have three files located within the /etc/nginx/sites-available/ directory, one, being the standard default file, and the other two being an SSL related config file for my domain. The proxy_pass directive seems simple enough. However, I keep getting a 502 Bad Gateway error when accessing the subdirectory. I’ve tried several combinations to see if my path was incorrect but to no avail. Is there anything within Ghost itself that needs to be configured to allow a Nginx Reverse Proxy to work? Here is my directive syntax:

location /path/ {
proxy_pass https://domain.com/path/;
}

I’ve tried specifying both http and https but either way, a 502 Bad Gateway error is still produced. I’ve also tried adding the directive to all three files but still doesn’t work. I’m adding the proxy_pass directive under the default location directive and I’m making sure that it is formatted correctly. I have also restarted Nginx.

Does anyone have any ideas on what might be wrong?

I greatly appreciate everyone’s insight!

for serving static files HTML/PDFs you don’t need to proxypass

First, create a directory where you want to put the static files.

& then open your nginx config

add the below location block

# Configuration for serving static files
    location /static {
        alias /path/to/static/dir;
        autoindex on;  # Enable directory listing if needed
    }

adjust the static files directory.

Save your nginx config

check if config is okay

sudo nginx -t

restart nginx

sudo systemctl restart nginx

hope this will solve your problem.

I gave this a try but no matter where I place the directive within the nginx.conf file, the check states that the location directive is not allowed:

nginx: [emerg] “location” directive is not allowed here in /etc/nginx/nginx.conf
nginx: configuration file /etc/nginx/nginx.conf test failed

Any additional thoughts? I really need to get this working somehow but I’m having a heck of a time. I’ve been pouring through nginx documentation but nothing clearly defines how to simply configure a directory with custom HTML. All of the examples don’t seem to work. :pensive:

could you share your currenct config ?

Yes, here is the entire nginx.conf file:

user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;

events {
	worker_connections 768;
	# multi_accept on;
}

http {

	##
	# Basic Settings
	##

	sendfile on;
	tcp_nopush on;
	types_hash_max_size 2048;
	# server_tokens off;

	# server_names_hash_bucket_size 64;
	# server_name_in_redirect off;

	include /etc/nginx/mime.types;
	default_type application/octet-stream;

	##
	# SSL Settings
	##

	ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3; # Dropping SSLv3, ref: POODLE
	ssl_prefer_server_ciphers on;

	##
	# Logging Settings
	##

	access_log /var/log/nginx/access.log;
	error_log /var/log/nginx/error.log;

	##
	# Gzip Settings
	##

	gzip on;

	# gzip_vary on;
	# gzip_proxied any;
	# gzip_comp_level 6;
	# gzip_buffers 16 8k;
	# gzip_http_version 1.1;
	# gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

	##
	# Virtual Host Configs
	##

	include /etc/nginx/conf.d/*.conf;
	include /etc/nginx/sites-enabled/*;
}

#mail {
#	# See sample authentication script at:
#	# http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
#
#	# auth_http localhost/auth.php;
#	# pop3_capabilities "TOP" "USER";
#	# imap_capabilities "IMAP4rev1" "UIDPLUS";
#
#	server {
#		listen     localhost:110;
#		protocol   pop3;
#		proxy      on;
#	}
#
#	server {
#		listen     localhost:143;
#		protocol   imap;
#		proxy      on;
#	}
#}

apologies I meant your domain config file.

something should be in /etc/nginx/sites-available/defaut or yourdomain.com

I have just reproduced it on my website…

here is my config file


server {
  listen 80;
  listen [::]:80;
  server_name thesaleheen.com www.thesaleheen.com;
  location / { return 301 https://thesaleheen.com; }
}

server {
  listen 443 ssl http2;
  listen [::]:443 ssl http2;
  server_name thesaleheen.com www.thesaleheen.com;

  ssl_protocols TLSv1.2;
  ssl_ciphers HIGH:!MEDIUM:!LOW:!aNULL:!NULL:!SHA;
  ssl_prefer_server_ciphers on;
  ssl_session_cache shared:SSL:10m;

  ssl_certificate     /etc/ssl/thesaleheen.com.crt;
  ssl_certificate_key /etc/ssl/thesaleheen.com.key;

location / {
                proxy_pass http://localhost:2368;
        }

## Static Location Block
 location /static {
        alias /home/ubuntu/static;
        autoindex on;  # Enable directory listing if needed
    }

client_max_body_size 100M;


}


I have put HTML file inside the static folder

& Here is the output

https://thesaleheen.com/static/

I have a total of three files within the /etc/nginx/sites-available/ directory which consists of the following:

visualdynamics.com-ssl.conf
visualdynamics.com.conf
default

I wasn’t sure which file to put the directive within so I have tried all three of the files but I only get an error.

I noticed that your alias path is different. I don’t have a /home/ubuntu/ directory so I was trying to specify the directory path for the www path which is located at /var/www/. I setup an example directory of “static” for this test so the full path is /var/www/static/. Oddly, there is also a directory called “html” located within the /var/www/ directory, so the full path is /var/www/html/static/. I’ve tried both the /var/www/ and /var/www/html/ diectories but to no avail.

Which file should I be adding the directive to? i.e.

visualdynamics.com-ssl.conf
visualdynamics.com.conf
default

My home directory consists of a second directory which is /ghost-mgr/ but there is no /ubuntu/ directory. :sob: I will try a directory within the /home/ directory in the next few minutes to see if that works.

Looks like adding the static HTML directory under the /home/ directory solved the problem. This really surprises me! I would have thought the proper directory should be /var/www/.

Also, I ended up adding the location directive to all three files:

visualdynamics.com-ssl.conf
visualdynamics.com.conf
default

Not sure if this is a good practice or not. I’m just thankful that it is working. My test directive looks like the following:

## Static Location Block
 location /static {
        alias /home/static;
        autoindex on;  # Enable directory listing if needed
    }

and it is successfully displaying at the following test URL:

However, I will remove the test URL shortly.

Salehin Khan, Thank you greatly for your insight! I truly appreciate it!

Happy to hear that. Cheers!

Make sure that the back-end server (in this case Ghost) is running and available. Make sure Ghost is configured to accept requests through a reverse proxy server. Sometimes Ghost may require additional configuration to work behind a proxy server. There are many places to buy proxies nowadays, but make sure they are of high quality. If you are using SSL, make sure your SSL certificates are configured correctly. The SSL configuration can be a separate file, and it is important that it is accurate. Check your server’s firewall settings to ensure that the required ports (80 and/or 443) are open and traffic is allowed.