美文网首页
nginx反向代理http/https

nginx反向代理http/https

作者: H_appiness | 来源:发表于2019-09-29 14:37 被阅读0次

记录一下nginx代理80、代理443端口的说明

端口 协议
80 http
443 https
  • 首先要安装nginx,这里采用yum安装方式
    yum -y install nginx
  • 安装完成后编辑其配置文件
    vim /etc/nginx/nginx.conf

配置代理80端口

    server {
        listen       80;
        server_name  www.prometheus.test.com; #自定义域名
        root         /usr/share/nginx/html;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        location / {
            proxy_pass http://192.168.100.158:9090; #填写对应主机IP
        }
    }

配置代理443端口

因https方式需要涉及到证书,我这里使用openssl自创建证书

openssl genrsa -out tls.key 2048
openssl req -new -x509 -days 365 -key tls.key -out tls.crt -subj /C=CN/ST=Beijingshi/L=Beijing/O=devops/CN=cn
    server {
        listen       80;
        listen       443 ssl;
        ssl_certificate /data/tls.crt; #找到证书所在对应目录
        ssl_certificate_key /data/tls.key; #找到key所在对应目录
        server_name  www.grafana.test.com; #自定义域名
        root         /usr/share/nginx/html;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        location / {
            proxy_pass http://192.168.100.158:3000; #填写对应主机IP
        }
    }

http自动跳转https

    server {
        listen       80;
        listen       443 ssl;
        ssl_certificate /data/tls.crt; #找到证书所在对应目录
        ssl_certificate_key /data/tls.key; #找到key所在对应目录
        server_name  www.prometheus.test.com; #自定义域名
        root         /usr/share/nginx/html;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;
        # http自动跳转https
        if ($server_port = 80 ) {
                return 301 https://$host$request_uri;
        }

        location / {
            proxy_pass http://192.168.100.158:9090; #填写对应主机IP
        }
        # http自动跳转https
        error_page 497  https://$host$request_uri;
    }

最后来进行测试
nginx -t
返回的信息如下即成功

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

重启nginx服务即可
systemctl restart nginx

相关文章

  • Nginx反向代理https

    Nginx对Https的反向代理 使用Nginx进行反向代理的时候,对于正常的http;流量使用 location...

  • nginx 代理https到http重定向失败的问题

    nginx 可以代理https到http,外网用https,内网使用http,这样的反向代理模式,安全简单。但是存...

  • nginx反向代理http/https

    记录一下nginx代理80、代理443端口的说明 首先要安装nginx,这里采用yum安装方式yum -y ins...

  • (转载)nginx反向代理TCP,取RTMP流

    nginx反向代理TCP,取RTMP流 一、说明nginx默认只支持HTTP反向代理,如果需要支持TCP反向代理需...

  • nginx

    node 使用 nginx 反向代理搭建https服务 使用自己生成的证书 nginx http配置 编辑ngin...

  • Nginx笔记 2018-01-16

    Nginx 常用功能 Http代理,反向代理:作为web服务器最常用的功能之一,尤其是反向代理。Nginx在做反向...

  • Nginx总结

    Nginx 安装 Nginx 是一款面向性能设计的 HTTP 服务器,能反向代理 HTTP,HTTPS 和邮件相关...

  • [nginx] 通过nginx反向代理给网站添加 https 证

    通过nginx反向代理给网站添加 https 证书。正常网站监听端口 8088 http 申请证书: https...

  • nginx配置说明

    Nginx常用功能1、Http代理,反向代理:作为web服务器最常用的功能之一,尤其是反向代理。Nginx在做反向...

  • Nginx中配置https做反向代理 - 知识林

    本文章来自【知识林】 在Centos中的Nginx配置https做反向代理跟配置http做反向代理基本一样,只是多...

网友评论

      本文标题:nginx反向代理http/https

      本文链接:https://www.haomeiwen.com/subject/ugqnectx.html