Nginx https反向代理

接前一篇文章,今天看看https的反向代理怎么配置。

生成自签名证书和私钥

要使用https,首先需要有证书和私钥,这里创建一个测试用的自签名证书和私钥。

使用 openssl 命令生成服务器私钥文件

shell 复制代码
openssl genrsa -out server.key 2048

生成证书请求

shell 复制代码
openssl req -new -key server.key -out server.csr

根据私钥和证书请求生成证书

shell 复制代码
openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt

配置反向代理

这里还是使用前一篇文章中使用的python3的 http server 作为后端应用服务。

shell 复制代码
$ python3 -m http.server 8000

修改 /etc/nginx/nginx.conf 文件,添加反向代理配置

shell 复制代码
    server {
		listen       443 ssl;
		server_name  localhost;
		
		ssl_certificate      /etc/nginx/server.crt;
		ssl_certificate_key  /etc/nginx/server.key;
		
		ssl_session_timeout  5m;
		
		ssl_protocols  TLSv1 TLSv1.1 TLSv1.2;
		ssl_prefer_server_ciphers   on;
		
		location / {
			proxy_pass              http://localhost:8000;
			proxy_http_version      1.1;
			proxy_set_header        Connection "";
			proxy_set_header        Host            $host;
			proxy_set_header        X-Real-IP       $remote_addr;
			proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
			proxy_set_header        X-Forwarded-Proto https;
		}
	}

修改后完整的 /etc/nginx/nginx.conf 文件内容如下:

shell 复制代码
user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;

events {
    worker_connections 768;
}

http {
    sendfile on;
    tcp_nopush on;
    types_hash_max_size 2048;

    include /etc/nginx/mime.types;
    default_type application/octet-stream;

    ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3;
    ssl_prefer_server_ciphers on;

    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;

    gzip on;

    server {
        listen 80;
        server_name localhost;
        location / {
            proxy_pass http://localhost:8000;
        }
    }

    server {
		listen       443 ssl;
		server_name  localhost;
		
		ssl_certificate      /etc/nginx/server.crt;
		ssl_certificate_key  /etc/nginx/server.key;
		
		ssl_session_timeout  5m;
		
		ssl_protocols  TLSv1 TLSv1.1 TLSv1.2;
		ssl_prefer_server_ciphers   on;
		
		location / {
			proxy_pass              http://localhost:8000;
			proxy_http_version      1.1;
			proxy_set_header        Connection "";
			proxy_set_header        Host            $host;
			proxy_set_header        X-Real-IP       $remote_addr;
			proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
			proxy_set_header        X-Forwarded-Proto https;
		}
	}

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;
}

重新启动服务使配置生效

shell 复制代码
$ sudo service nginx restart

最后通过访问 "https://localhost" 地址检查请求是否被代理到后端的python应用上了。

相关推荐
ezreal_pan2 小时前
Kubernetes 负载均衡现象解析:为何同一批次请求集中于单个 Pod
运维·云原生·k8s·traefik
朱皮皮呀2 小时前
Spring Cloud——服务注册与服务发现原理与实现
运维·spring cloud·eureka·服务发现·php
云边云科技4 小时前
零售行业新店网络零接触部署场景下,如何选择SDWAN
运维·服务器·网络·人工智能·安全·边缘计算·零售
城管不管4 小时前
Docker核心---数据卷(堵门秘籍)
运维·docker·容器
AOwhisky4 小时前
Linux 文本处理三剑客:awk、grep、sed 完全指南
linux·运维·服务器·网络·云计算·运维开发
xuanerya5 小时前
使用 SSH 方式克隆 GitHub 仓库没有权限解决办法
运维·ssh·github
2501_916007475 小时前
iOS App 上架实战 从内测到应用商店发布的全周期流程解析
android·ios·小程序·https·uni-app·iphone·webview
.Shu.5 小时前
计算机网络 HTTPS 全流程
网络协议·计算机网络·https
runfarther5 小时前
搭建LLaMA-Factory环境
linux·运维·服务器·python·自然语言处理·ai编程·llama-factory
百思可瑞教育6 小时前
Spring Cloud Gateway 负载均衡全面指南
运维·负载均衡·北京百思可瑞教育·百思可瑞教育·北京百思教育