配置nginx反向代理

2022年2月22日 1636点热度 0人点赞 0条评论

利用 Nginx 作为反向代理服务器,即可方便的给 需要反代的网站单独绑定一个我们希望的域名,并利用Let's Encrypt 来部署 HTTPS。

假设我在之前的docker-compose.yml里面,我填好的地址是https://rss.yirenliu.cn

这里,我在宝塔面板新建一个站点:

不要数据库,不要php,纯静态即可

配置好SSL证书:

免费证书Let's Encrypt,宝塔会自动续期

修改后部分的配置文件:

注意观察

# location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
    # {
    #     expires      30d;
    #     error_log off;
    #     access_log /dev/null;
    # }
    
    # location ~ .*\.(js|css)?$
    # {
    #     expires      12h;
    #     error_log off;
    #     access_log /dev/null; 
    # }
                location / {
    proxy_pass http://127.0.0.1:8002/;
    rewrite ^/(.*)$ /$1 break;
    proxy_redirect off;
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Upgrade-Insecure-Requests 1;
    proxy_set_header X-Forwarded-Proto https;
  }
    access_log  /www/wwwlogs/rss.yirenliu.cn.log;
    error_log  /www/wwwlogs/rss.yirenliu.cn.error.log;
}

主要是注释了52-64的内容:

    # location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
    # {
    #     expires      30d;
    #     error_log off;
    #     access_log /dev/null;
    # }
    
    # location ~ .*\.(js|css)?$
    # {
    #     expires      12h;
    #     error_log off;
    #     access_log /dev/null; 
    # }


添加了65-75行的内容,


location / {
    proxy_pass http://127.0.0.1:8002/;
    rewrite ^/(.*)$ /$1 break;
    proxy_redirect off;
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Upgrade-Insecure-Requests 1;
    proxy_set_header X-Forwarded-Proto https;
  }

端口改成你自己在docker-compose.yml设置的端口即可。

注意:这个方法可以解决宝塔面板默认只能配置一个反向代理的情况!!!

hank

这个人很懒,什么都没留下

文章评论