如何将Nginx配置为proxy_pass? [英] How to configure nginx as proxy_pass?

查看:123
本文介绍了如何将Nginx配置为proxy_pass?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用Certbot Nginx将我的Nginx安装到Ubuntu 18.04.我还使用Certbot来获取LetsEncrypt证书.

我使用基本配置,例如使用tcp 80和433,因此可以同时获取http和https.大多数配置由Certbot完成.

我的域为 http://example.com ,并提供静态页面. Bu我也有一个文件夹,我可以使用 http://example.com/myfolder 来命名.

安装nginx后,我尝试对本地rest_api服务使用proxy_pass和https.

如果我输入 http://127.0.0.1:1024/myfolder ,那么我可以看到我的rest-api的作品. 如果我输入 https://example.com/myfolder ,则nginx不会激活proxy_pass. mystic.com已经注册,并且可以工作8个月以上.所有dns配置均正常运行.另外,nginx letsencrypt也可以正常工作. 1周前,我的证书会自动更新(每90天).

我的Nginx配置如下.我的nginx版本是1.15.10.

  1. 如何将nginx配置为proxy_pass?
  2. 我的proxy_pass是否正确?
  3. 如何更改此行try_files $uri $uri/ =404;以显示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/;
       }
    }

解决方案

尝试像这样更改基本的Nginx配置,然后从certbot安装LetsEncrypt证书:

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";

    }
}

它对certbot来说非常适合我.不要忘记在测试配置之前重新加载nginx服务.

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/;
       }
    }

解决方案

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.

这篇关于如何将Nginx配置为proxy_pass?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆