阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

Nginx自签名https和反向代理

167次阅读
没有评论

共计 2711 个字符,预计需要花费 7 分钟才能阅读完成。

场景

公司的 wiki 服务器和 docker private registry 都在公司的桌面云里,由于公网 IP 资源紧张,无法为这些服务器每个都配上公网 IP,只能通过一个公网 IP 来访问,所以需要用 Nginx 做个反向代理来访问些服务器。另外,这些服务都要以 https 来访问。

服务器 内网 IP
wiki.renhl.com 172.168.100.47
hub.renhl.com 172.168.100.48

生成自签名的证书

因为是自己公司用也就无需申请认证的证书了,自签名即可。

$ sudo mkdir -p /etc/nginx/ssl
$ sudo openssl req -x509 -nodes -days 3650 -newkey rsa:2048 -keyout /etc/nginx/ssl/nginx.key -out /etc/nginx/ssl/nginx.crt

配置反向代理

编辑/etc/nginx/sites-available/default, 加入如下内容:

upstream wiki  {server 172.168.100.47:80; # wiki.renhl.com}

upstream hub  {server 172.168.100.48; # hub.renhl.com}

## Start wiki.renhl.com ##
server {

    listen 80;

    listen 443 ssl;

    ssl_certificate /etc/nginx/ssl/nginx.crt;
    ssl_certificate_key /etc/nginx/ssl/nginx.key;

    server_name  wiki.ecloud.com.cn;

    access_log  /var/log/nginx/wiki.renhl.access.log;
    error_log  /var/log/nginx/wiki.renhl.error.log;
    root   /usr/share/nginx/html;
    index  index.html index.htm;

    ## send request back to apache1 ##
    location / {
     proxy_pass  http://wiki;
     proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
     proxy_redirect off;
     proxy_buffering off;
     proxy_set_header        Host            $host;
     proxy_set_header        X-Real-IP       $remote_addr;
     proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
   }
}
## End wiki.renhl.com ##

## START hub.renhl.com ##
server {
    server_name hub.renhl.com;

    listen 80;
    listen 443 ssl;

    ssl_certificate /etc/nginx/ssl/nginx.crt;
    ssl_certificate_key /etc/nginx/ssl/nginx.key;

   access_log  /var/log/nginx/hub.renhl.access.log;
   error_log   /var/log/nginx/hub.renhl.error.log;
   root        /usr/local/nginx/html;
   index       index.html;

   location / {
        proxy_pass  https://hub;
        proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
        proxy_redirect off;
        proxy_buffering off;
        proxy_set_header        Host           $host;
        proxy_set_header        X-Real-IP       $remote_addr;
        proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}
## END hub.renhl.com  ##

IP 限制

出于安全的考虑,要禁止公司以外的人访问这些服务,在 nginx 里设置只允许公司的 IP 访问。在上面的两个配置里加入下面的内容:

allow 111.206.238.12;
allow 111.206.238.94;
deny all;

參考文献

  • https://www.digitalocean.com/community/tutorials/how-to-create-an-ssl-certificate-on-nginx-for-ubuntu-14-04
  • http://www.cyberciti.biz/tips/using-nginx-as-reverse-proxy.html

更多 Nginx 相关教程见以下内容

CentOS 6.2 实战部署 Nginx+MySQL+PHP http://www.linuxidc.com/Linux/2013-09/90020.htm

使用 Nginx 搭建 WEB 服务器 http://www.linuxidc.com/Linux/2013-09/89768.htm

搭建基于 Linux6.3+Nginx1.2+PHP5+MySQL5.5 的 Web 服务器全过程 http://www.linuxidc.com/Linux/2013-09/89692.htm

CentOS 6.3 下 Nginx 性能调优 http://www.linuxidc.com/Linux/2013-09/89656.htm

CentOS 6.3 下配置 Nginx 加载 ngx_pagespeed 模块 http://www.linuxidc.com/Linux/2013-09/89657.htm

CentOS 6.4 安装配置 Nginx+Pcre+php-fpm http://www.linuxidc.com/Linux/2013-08/88984.htm

Nginx 安装配置使用详细笔记 http://www.linuxidc.com/Linux/2014-07/104499.htm

Nginx 日志过滤 使用 ngx_log_if 不记录特定日志 http://www.linuxidc.com/Linux/2014-07/104686.htm

Nginx 的详细介绍:请点这里
Nginx 的下载地址:请点这里

本文永久更新链接地址:http://www.linuxidc.com/Linux/2015-12/126226.htm

正文完
星哥说事-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2022-01-21发表,共计2711字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中