Skip to content

Setup Nginx on Google Cloud

Pulkit Singhal edited this page Aug 21, 2015 · 23 revisions

Setup Nginx

Install Nginx

  1. Update apt

sudo apt-get update ```

  1. Install nginx

sudo apt-get install nginx ```

Install SSL-Certificate

  1. Get SSL-Certificate from provider.
  • DNSimple: Domains > drill down into your domain > Domain tab on left side > SSL Certificates header > drill down into your certificate > Certificate section > Install the SSL certificate > Enjoy the crisp / clear instructions
  1. Save .key and .pem in server
  • ex: /etc/nginx/ssl

  • Example:

$ gcloud compute instances list $ gcloud compute copy-files ~/Downloads/MY_DOMAIN.key root@INSTANCE_NAME:/home/myUsername --zone INSTANCE_ZONE $ gcloud compute copy-files ~/Downloads/MY_DOMAIN.pem root@INSTANCE_NAME:/home/myUsername --zone INSTANCE_ZONE ```

  • blah
  1. Add a new virtual host or edit default

  2. Edit host file

vim /etc/nginx/sites-available/default ```

  1. Redirect http traffic to https

server { listen 80; return 301 https://$host$request_uri; } ```

  1. Add ssl certificate

server {

listen 443;

ssl on; ssl_certificate /etc/nginx/ssl/<CERTIFICATE_PEM>; ssl_certificate_key /etc/nginx/ssl/<CERTIFICATE_KEY>;

server_name <DOMAIN_NAME>; #Redirect application port to https location / {

   proxy_set_header        Host $host;
   proxy_set_header        X-Real-IP $remote_addr;
   proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
   proxy_set_header        X-Forwarded-Proto $scheme;

   # Fix the “It appears that your reverse proxy set up is broken" error.
   proxy_pass          http://localhost:<APPLICATION_PORT>;
   proxy_read_timeout  90;
   proxy_redirect      http://localhost:<APPLICATION_PORT> https://<DOMAIN_NAME>;

} } ```

  1. Restart nginx

/etc/init.d/nginx restart ```

Troubleshoot Nginx

  1. All the files should be easily listable: ls -alrt /etc/nginx/
  2. Have a peek at cat /etc/nginx/sites-available/default to make sure that all is as you think it should be
  3. You can also peek at cat /etc/nginx/nginx.conf to see where the log files for nginx go:
  4. /var/log/nginx/access.log
  5. /var/log/nginx/error.log
  6. and tail or less those files if needed for troubleshooting

Clone this wiki locally