使用SSL配置Nginx反向代理的简单指南

服务器
反向代理是一个服务器,它接收通过Web发出的请求,即http和https,然后将它们发送到后端服务器(或服务器)。后端服务器可以是单个或一组应用服务器,如Tomcat,wildfly或Jenkins等,或者甚至可以是其他Web服务器,如Apache等。

 反向代理是一个服务器,它接收通过Web发出的请求,即http和https,然后将它们发送到后端服务器(或服务器)。后端服务器可以是单个或一组应用服务器,如Tomcat,wildfly或Jenkins等,或者甚至可以是其他Web服务器,如Apache等。

我们已经讨论过如何使用Nginx配置简单的http反向代理。在本教程中,我们将讨论如何使用SSL配置Nginx反向代理。因此,让我们从使用SSL配置Nginx反向代理的过程开始。

先决条件

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 
  • 1.

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

安装Nginx

Ubuntu

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

$ sudo apt-get update && sudo apt-get install nginx 
  • 1.

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

# systemctl start nginx 
 
# systemctl enable nginx 
  • 1.
  • 2.
  • 3.

现在检查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; 
 

 

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.

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

第1节

server { 
listen 80;  
return 301 https://$host$request_uri; 

  • 1.
  • 2.
  • 3.
  • 4.

在这里,我们告诉我们要听取对端口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
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.

现在这些是我们正在使用的一些默认的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; 
 

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.

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

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

# nginx -t 
  • 1.

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

# systemctl restart nginx 
  • 1.

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

这完成了我们如何使用ssl配置nginx反向代理的教程,请使用下面的注释框发送有关本教程的任何问题或疑问。

责任编辑:武晓燕 来源: 程序猿knight
相关推荐

2022-07-18 05:57:35

SSL 证书数据安全

2023-10-17 08:36:28

Nginx代理服务器

2022-07-01 07:33:24

nginx反向代理测试

2014-04-29 14:54:48

Nginx反向代理

2015-06-05 11:26:58

nginx运维

2011-08-30 11:32:53

UbuntuNginx

2012-12-07 10:14:48

Nginx负载均衡

2024-08-07 14:56:00

Nginx反向代理配置

2018-11-12 12:17:00

2023-12-05 09:14:54

2024-07-22 15:34:25

2010-03-30 14:35:58

Nginx反向代理

2020-10-22 08:05:46

Nginx

2019-07-09 15:10:02

Nginx反向代理负载均衡

2017-12-18 12:04:02

Nginx代理均衡

2019-06-19 15:34:39

Nginx反向代理负载均衡

2020-08-06 08:23:24

Nginx反向代理Web安全

2023-09-13 07:16:31

Ngnix代理服务器

2020-10-31 21:40:35

物联网网关物联网IOT

2016-09-07 18:57:48

点赞
收藏

51CTO技术栈公众号