如何利用SSL配置Nginx反向代理

您所在的位置:网站首页 nginx反向代理配置宝塔 如何利用SSL配置Nginx反向代理

如何利用SSL配置Nginx反向代理

2023-05-14 13:44| 来源: 网络整理| 查看: 265

先决条件

1.后端服务器:为了本教程的目的,我们使用在端口8080的localhost上运行的tomcat服务器

注意: - 当您开始代理请求时,请确保应用程序服务器已启动。

2.ssl证书:我们还需要在服务器上配置ssl证书。我们可以使用 let's encrypt的加密证书,你可以使用这里提到的程序得到一个。但是对于本教程,我们将使用自签名证书,可以通过从终端运行以下命令来创建,

$ openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout /etc/nginx/certs/cert.key -out /etc/nginx/certs/cert.crt登录后复制

使用ssl配置nginx反向代理的下一步将是nginx安装,

安装nginx

ubuntu

nginx可用于默认的ubuntu存储库。这么简单,使用以下命令安装它,

$ sudo apt-get update && sudo apt-get install nginx登录后复制

现在启动服务并启用它以进行启动,

# systemctl start nginx # systemctl enable nginx登录后复制

现在检查nginx安装,我们可以打开web浏览器并输入系统ip作为url以获取默认的nginx网页,这确认nginx工作正常。

使用ssl配置nginx反向代理

现在我们拥有了使用ssl配置nginx反向代理所需的所有东西。我们现在需要在nginx中进行配置,我们将使用默认的nginx配置文件,即/etc/nginx/conf.d/default.conf.

假设这是我们第一次对配置进行任何更改,打开文件并删除或注释所有旧文件内容,然后将以下条目放入文件中。

vi /etc/nginx/conf.d/default.conf

server { listen 80; return 301 https://$host$request_uri; } server { listen 443; server_name linuxtechlab.com; ssl_certificate /etc/nginx/ssl/cert.crt; ssl_certificate_key /etc/nginx/ssl/cert.key; ssl on; ssl_session_cache builtin:1000 shared:ssl:10m; ssl_protocols tlsv1 tlsv1.1 tlsv1.2; ssl_ciphers high:!anull:!enull:!export:!camellia:!des:!md5:!psk:!rc4; ssl_prefer_server_ciphers on; access_log /var/log/nginx/access.log; location / { 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 $scheme; proxy_pass http://localhost:8080; proxy_read_timeout 90; proxy_redirect http://localhost:8080 https://linuxtechlab.com; } }登录后复制

完成所有更改后,保存文件并退出。在我们重新启动nginx服务以实现所做的更改之前,我们将逐节讨论我们所做的配置。

第1节

server { listen 80; return 301 https://$host$request_uri; }登录后复制

在这里,我们告诉我们要听取对端口80的任何请求,然后将其重定向到https。

第2节

listen 443; server_name linuxtechlab.com; ssl_certificate /etc/nginx/ssl/cert.crt; ssl_certificate_key /etc/nginx/ssl/cert.key; ssl on; ssl_session_cache builtin:1000 shared:ssl:10m; ssl_protocols tlsv1 tlsv1.1 tlsv1.2; ssl_ciphers high:!anull:!enull:!export:!camellia:!des:!md5:!psk:!rc4; ssl_prefer_server_ciphers on;登录后复制

现在这些是我们正在使用的一些默认的nginx ssl选项,它们告诉nginx web服务器支持哪种协议版本,ssl密码。

第3节

location / { 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 $scheme; proxy_pass http://localhost:8080; proxy_read_timeout 90; proxy_redirect http://localhost:8080 https://linuxtechlab.com; }登录后复制

现在,本节介绍代理以及传入请求进入后的位置。现在我们已经讨论了所有配置,我们将检查然后重新启动nginx服务。

要检查nginx,请运行以下命令

# nginx -t登录后复制

一旦我们所有配置文件都ok,我们将重新启动nginx服务

# systemctl restart nginx登录后复制

就是这样,我们的ssl nginx反向代理现已准备就绪。现在要测试设置,您所要做的就是打开web浏览器并输入url。我们现在应该重定向到apache tomcat网页。

以上就是如何利用SSL配置Nginx反向代理的详细内容,更多请关注php中文网其它相关文章!



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3