Nick
Nick

Reputation: 4483

How to configure nginx as proxy_pass?

I have use Certbot Nginx to install my nginx to Ubuntu 18.04. I also use Certbot to get the LetsEncrypt certificate.

I use basic configuration such as use tcp 80 and 433 so I can get both http and https. Most of the configuration done by Certbot.

my domain is http://example.com and provides static page. Bu I also have a folder and I can call it with http://example.com/myfolder.

After I install nginx I try to use proxy_pass and https to my local rest_api services.

If I type http://127.0.0.1:1024/myfolder then I can see my rest-api works. If I type https://example.com/myfolder than nginx is not activate proxy_pass. mystic.com has registered and it works more than 8 months. All the dns configuration is working. Also nginx letsencrypt is working. 1 week ago my certificate automatically update itself (every 90 days).

My Nginx Configuration is below. my nginx version is 1.15.10.

  1. How to configure nginx as proxy_pass?
  2. Is my proxy_pass is correct?
  3. how can I change this line try_files $uri $uri/ =404; to show var/www/html/index.html?
    server {
        charset UTF-8;
        listen 80 ;
        listen [::]:80 ;

        server_name example.com; # managed by Certbot    

        if ($host = example.com) {
            return 301 https://$host$request_uri;
        } # managed by Certbot
        return 404; # managed by Certbot
    }

    server {
        charset UTF-8;
        listen [::]:443 ssl ipv6only=on; # managed by Certbot
        listen 443 ssl; # managed by Certbot
        server_name example.com; # managed by Certbot

        ssl_certificate /mypath/example.com/fullchain.pem; # managed by Certbot
        ssl_certificate_key /mypath/example.com/privkey.pem; # managed by Certbot
        include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
        ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

        location / {
        # First attempt to serve request as file, then
        # as directory, then fall back to displaying a 404.
            try_files $uri $uri/ =404;
            proxy_pass http://127.0.0.1:1024/;
       }
    }

Upvotes: 3

Views: 11689

Answers (1)

Rajan Sharma
Rajan Sharma

Reputation: 2273

Try changing your basic Nginx configuration like this and then install the LetsEncrypt certificate from certbot :

server {
    listen 80;
    server_name example.com;
    location / {
        proxy_set_header   X-Forwarded-For $remote_addr;
        proxy_set_header   Host $http_host;
        proxy_pass         "http://127.0.0.1:1024";

    }
   location /admin {
        proxy_set_header   X-Forwarded-For $remote_addr;
        proxy_set_header   Host $http_host;
        proxy_pass         "http://192.168.2.1:8080";

    }
   location /user {
        proxy_set_header   X-Forwarded-For $remote_addr;
        proxy_set_header   Host $http_host;
        proxy_pass         "http://192.168.2.1:1024";

    }
}

It worked perfectly for me with certbot.Don't forget to reload the nginx service before testing the configuration.

Upvotes: 8

Related Questions