Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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/;
       }
    }
like image 990
Nick Avatar asked Nov 01 '25 02:11

Nick


1 Answers

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.

like image 65
Rajan Sharma Avatar answered Nov 04 '25 10:11

Rajan Sharma



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!