Nginx安装配置与SSL证书安装部署

一、Nginx

Nginx是一款高性能的开源Web服务器和反向代理服务器,被广泛用于构建现代化的Web应用和提供静态内容。

nginx官网
这里下载nginx-1.24.0-zip
在这里插入图片描述

Nginx是一款高性能的开源Web服务器和反向代理服务器,被广泛用于构建现代化的Web应用和提供静态内容。
Nginx是位于互联网和后端基础设置之间的网关,当你访问网络应用程序时,请求(request)会首先发送到网络服务器,Nginx会查看请求的资源并确定资源在服务器上的位置,然后将其作为响应(response)发送回客户端(浏览器)。

在浏览器的开发者工具中,依次点击Network,Headers,和Name中的资源,即可在Response headers中(即响应的服务器标头)找到Server:nginx。(2023-11-17_181848-server为nginx.png)
在这里插入图片描述

Nginx在高流量站点中非常受欢迎,因为它的事件驱动架构,大概可以同时处理超过10000个同时连接。
Nginx常用来做反向代理,充当指挥中心(红绿灯),将负载合理分配到多个后端服务器,同时还提供安全性和缓存,以实现更好的性能。

二、CentOS7.9 2009下安装配置nginx

CentOS7.9 2009下安装配置nginx可参考我这篇文章:
Windows11与CentOS7.9 2009下安装配置nginx后启动整个项目中的 二、CentOS7.9 2009下安装配置nginx

完整安装命令如下(完整安装命令的执行过程截图见上方博客链接):

//1-安装依赖项
sudo yum install gcc pcre-devel zlib-devel openssl-devel//2-下载Nginx源码包
//2-1-下载Nginx源码包
从https://nginx.org/en/download.html查找最新的版本,使用Stable version版本。 
nginx-1.24.0.tar.gz
//2-2--将下载好的tar包上传到centos701的目录中  /usr/local   nginx-1.24.0.tar.gz或者//2-下载Nginx源码包
//或者直接复制tar包链接(在Windows中右击复制tar包下载链接)//2-1 CentOS系统安装wget
yum install wget -y
//2-2 Debian/Ubuntu系统安装wget,需要执行以下命令:
apt-get install -y wget//2-2-使用wget获取nginx-1.24.0.tar.gz
wget https://nginx.org/download/nginx-1.24.0.tar.gz3、安装
//3-1、解压安装包nginx-1.24.0.tar.gz
tar -zxvf nginx-1.24.0.tar.gz//3-2、进入解压后的目录nginx-1.24.0
cd  nginx-1.24.0/4、 配置编译选项,包括启用HTTPS支持
//注意这里的目录就是/usr/local/nginx1.24.0
//如果报错就执行4-1、安装依赖项
//说明:因为后续需要安装SSL证书,所以添加了几个模块;如果不需要,可以执行./configure 。
./configure --prefix=/usr/local/nginx1.24.0 --with-http_ssl_module --with-http_v2_module --with-http_stub_status_module参数说明:
--prefix=path:默认路径为/usr/local/nginx,可不配置。
--with-http_ssl_module:配置Nginx以启用HTTPS模块。
--with-http_v2_module:配置Nginx以启用HTTP2模块。
--with-http_stub_status_module:启用状态监控模块,允许查看Nginx的运行状态和统计信息。说明:因为后续需要安装SSL证书,所以添加了几个模块;如果不需要,可以执行./configure 。//4-1、安装依赖项
执行yum -y install gcc openssl-devel pcre-devel zlib-devel 
----
报错:2023-9-5 02:33:41
[root@localhost nginx-1.24.0]# ./configure --prefix=/usr/local/nginx1.24.0 --with-http_ssl_module --with-http_v2_module --with-http_stub_status_module
checking for OS+ Linux 3.10.0-1160.el7.x86_64 x86_64
checking for C compiler ... not found./configure: error: C compiler cc is not found执行yum -y install gcc openssl-devel pcre-devel zlib-devel 后,
再次执行./configure --prefix=/usr/local/nginx1.24.0 --with-http_ssl_module --with-http_v2_module --with-http_stub_status_module 
不会再报错。
----//4-2、编译和安装Nginx
//编译安装完成后,会发现多了一个nginx1.24.0文件夹
make && make install  或者分别执行//4-2、编译和安装Nginx
//编译安装完成后,会发现多了一个nginx1.24.0文件夹
//编译
make
//安装
sudo make install//5、启动Nginx
//关闭防火墙
systemctl stop firewalld或systemctl stop firewalld.service//进入nginx-1.24.0编译和安装生成的目录nginx1.24.0/sbin
cd /usr/local/nginx1.24.0/sbin//启动nginx
./nginx或者
sudo nginx//6、查看nginx的进程
yum install net-tools -y
netstat -lntup | grep nginx//查看启动的nginx进程
ps -ef | grep nginxyum install lsof  
lsof -i :80//7、关闭nginx:
./nginx -s stop

三、nginx.conf的配置

3.1 用户名和日志

在大多数情况下,Nginx安装在linux服务器上,配置文件nginx.conf位于conf目录下(例如/usr/local/nginx1.24.0/conf/nginx.conf),可以在nginx.conf文件中,通过配置指令值,来指定服务器的行为。

指令值是键值对(key value),后跟大括号,例如http{ },则为上下文context,内部包含更多指令。

在全局上下文(main或者global context)(即http{}外面部分)中,可以设置用户名user,和错误日志error_log等内容。但大多数配置都在http{}上下文context中完成。
注意:在nginx.conf中,#号表示注释,不生效。

#user值为用户名
user  nobody;
#error_log 为错误日志
error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

3.2 配置静态内容

Nginx提供静态内容,例如图像和HTML,可以在http上下文context中处理这个问题。在其中定义一个或者多个服务器,每个服务器通过端口来区分。

Nginx将跟踪对服务器的每个请求,可以将其写入访问日志access.log。最重要的是告诉服务器在那里可以找到原始文件,在位置上下文location{}中配置此操作。

现在当用户导航到我们的服务器域名或者服务器IP时,知道文件系统哪里可以找到index.html文件,并且我们可以根据需要设置第二个位置,来将任何图像的格式与目录相匹配。

#user值为用户名
#在全局上下文(main或者global context)(即http{}外面部分)
#user  nobody;
#error_log 为错误日志
#error_log  logs/error.log;#此处为http上下文context外,在全局上下文(main或者global context)(即http{}外面部分) 中http{#此处为http上下文context中server{#端口listen 80;#访问日志access.logaccess_log /var/log/nginx/access.loglocation / {#/app/www 文件夹有index.html文件。root /app/www }location ~ \.(gif|jpg|png)${#并且我们可以需要设置第二个位置,来将任何图像的格式与目录相匹配。#/app/images目录中有png等格式的图片root /app/images}}
}

在服务器配置nginx.conf中处理的其他常见事项大概包括,配置SSL证书,重写和路由到代理服务器,
当使用代理服务器(proxy_pass)替换root时,可以指向不同的服务器。

#user值为用户名
#在全局上下文(main或者global context)(即http{}外面部分)
#user  nobody;
#error_log 为错误日志
#error_log  logs/error.log;#此处为http上下文context外,在全局上下文(main或者global context)(即http{}外面部分) 中http{#此处为http上下文context中server{#端口listen 80;#访问日志access.logaccess_log /var/log/nginx/access.loglocation / {#/app/www 文件夹有index.html文件。#root /app/www #当使用代理服务器(proxy_pass)替换root时,可以指向不同的服务器proxy_pass http://192.66.55.44:2096}}}//2023-11-17 19:07:14
//创建一个反向代理,可以处理缓存,匿名,和负载平衡。server {listen 2096;root /app/www;location /{}}

四、nginx.conf

4.1 默认的nginx.conf的配置

#user  nobody;
worker_processes  1;#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;#pid        logs/nginx.pid;events {worker_connections  1024;
}http {include       mime.types;default_type  application/octet-stream;#log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '#                  '$status $body_bytes_sent "$http_referer" '#                  '"$http_user_agent" "$http_x_forwarded_for"';#access_log  logs/access.log  main;sendfile        on;#tcp_nopush     on;#keepalive_timeout  0;keepalive_timeout  65;#gzip  on;server {listen       80;server_name  localhost;#charset koi8-r;#access_log  logs/host.access.log  main;location / {root   html;index  index.html index.htm;}#error_page  404              /404.html;# redirect server error pages to the static page /50x.html#error_page   500 502 503 504  /50x.html;location = /50x.html {root   html;}# proxy the PHP scripts to Apache listening on 127.0.0.1:80##location ~ \.php$ {#    proxy_pass   http://127.0.0.1;#}# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000##location ~ \.php$ {#    root           html;#    fastcgi_pass   127.0.0.1:9000;#    fastcgi_index  index.php;#    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;#    include        fastcgi_params;#}# deny access to .htaccess files, if Apache's document root# concurs with nginx's one##location ~ /\.ht {#    deny  all;#}}# another virtual host using mix of IP-, name-, and port-based configuration##server {#    listen       8000;#    listen       somename:8080;#    server_name  somename  alias  another.alias;#    location / {#        root   html;#        index  index.html index.htm;#    }#}# HTTPS server##server {#    listen       443 ssl;#    server_name  localhost;#    ssl_certificate      cert.pem;#    ssl_certificate_key  cert.key;#    ssl_session_cache    shared:SSL:1m;#    ssl_session_timeout  5m;#    ssl_ciphers  HIGH:!aNULL:!MD5;#    ssl_prefer_server_ciphers  on;#    location / {#        root   html;#        index  index.html index.htm;#    }#}}

4.2 /目录中html目录下的dist的nginx.conf配置

在服务器的/目录下新建文件夹html,将dist文件夹上传到此html目录中
dist文件夹为前端打好的包。

dist,即distribution,是Vue打包后生成的默认目录名称,里面包含了前端项目所需要的HTML、CSS、JavaScript等资源文件。

npm run build 一般来说都是这个打包命令。

打包命令根据package.json文件中的命令中的配置来写,下面的打包命令就是npm run build:prod,prod是production的缩写,意思是打生产环境的包,其中的 SET NODE_OPTIONS=--openssl-legacy-provider 是为了解决Node.js版本过高的问题(Error:0308010C:digital envelope routines::unsupported)而添加的。
详情见我这篇文章:
IDEA中Node.js环境下npm报错Error:0308010C:digital envelope routines:unsupported

npm相关安装配置等,参考我这篇文章:
安装配置nvm-windows对Node.js与npm进行版本控制

package.json文件中的命令中配置的打包命令

  "scripts": {"dev": "SET NODE_OPTIONS=--openssl-legacy-provider && vue-cli-service serve","build:prod": "SET NODE_OPTIONS=--openssl-legacy-provider && vue-cli-service build","preview": "node build/index.js --preview","lint": "eslint --ext .js,.vue src"},

注意:yarn init或者npm init,执行输入信息后,会生成package.json文件。
参考我这篇文章:安装并配置使用包管理工具-Yarn

启动防火墙同时开放nginx80端口:
参考我这篇 查看CentOS版本及系统位数与设置CentOS 7.9 2009 防火墙配置放开端口的命令与过程

//禁止防火墙开机启动。这种方法方便,但不安全。
systemctl disable firewalld下面  设置CentOS7.9 2009 防火墙配置放开端口80//1-防火墙设置开机自启
systemctl enable firewalld//2-查询防火墙状态
systemctl status firewalld//3-启动防火墙
systemctl start firewalld
//关闭防火墙
systemctl stop firewalld//4-查询防火墙状态,确保已经启动active(running)
systemctl status firewalld//5-查看防火墙是否放行nginx80端口 no:代表没开80端口,yes表示已开nginx80端口。
firewall-cmd --query-port=80/tcp//6-设置永久放行Nginx80端口,”–permanent“参数表示,永久生效,没有此参数重启后失效,表示重启后80端口无法通过。
firewall-cmd --add-port=80/tcp --permanent//7-【设置永久放行Nginx80端口,重启防火墙后才能查询到防火墙已经放行Nginx80端口。】
//返回no,需要重启防火墙才能更新为yes
firewall-cmd --query-port=80/tcp
//#重启防火墙
systemctl restart firewalld
//返回yes,表示永久放行Nginx80端口
//重启防火墙后再次查看就是yes,表示80端口已经永久放行
firewall-cmd --query-port=80/tcp

/目录中html目录下的dist的nginx.conf配置如下:


#user  nobody;
worker_processes  1;#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;#pid        logs/nginx.pid;events {worker_connections  1024;
}http {include       mime.types;default_type  application/octet-stream;#log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '#                  '$status $body_bytes_sent "$http_referer" '#                  '"$http_user_agent" "$http_x_forwarded_for"';#access_log  logs/access.log  main;sendfile        on;#tcp_nopush     on;#keepalive_timeout  0;keepalive_timeout  65;#gzip  on;server {#多个nginx,指定不同端口(设置访问端口)listen       8083;server_name  localhost;#charset koi8-r;#access_log  logs/host.access.log  main;location / {root  /html/dist;index  index.html index.htm;try_files $uri $uri/ /index.html;}#2023-8-9 02:30:13location /api{rewrite  ^/api/(.*)$ /$1 break;proxy_pass http://localhost:9092;}#error_page  404              /404.html;# redirect server error pages to the static page /50x.html#error_page   500 502 503 504  /50x.html;location = /50x.html {root   html;}# proxy the PHP scripts to Apache listening on 127.0.0.1:80##location ~ \.php$ {#    proxy_pass   http://127.0.0.1;#}# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000##location ~ \.php$ {#    root           html;#    fastcgi_pass   127.0.0.1:9000;#    fastcgi_index  index.php;#    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;#    include        fastcgi_params;#}# deny access to .htaccess files, if Apache's document root# concurs with nginx's one##location ~ /\.ht {#    deny  all;#}}# another virtual host using mix of IP-, name-, and port-based configuration##server {#    listen       8000;#    listen       somename:8080;#    server_name  somename  alias  another.alias;#    location / {#        root   html;#        index  index.html index.htm;#    }#}# HTTPS server##server {#    listen       443 ssl;#    server_name  localhost;#    ssl_certificate      cert.pem;#    ssl_certificate_key  cert.key;#    ssl_session_cache    shared:SSL:1m;#    ssl_session_timeout  5m;#    ssl_ciphers  HIGH:!aNULL:!MD5;#    ssl_prefer_server_ciphers  on;#    location / {#        root   html;#        index  index.html index.htm;#    }#}}

访问成功:
在这里插入图片描述

4.3 将dist包上传到指定目录/deploy/ruoyicloud_ui/中

参考我的这篇博客:[Windows11与CentOS7.9 2009下安装配置nginx后启动整个项目]
中的 三、配置CentOS7.9 2009中nginx开机自启 与 4.4 部署dist包 。

4.3.1 配置CentOS7.9 2009中nginx开机自启

参考我的这篇博客:[Windows11与CentOS7.9 2009下安装配置nginx后启动整个项目]
中的 三、配置CentOS7.9 2009中nginx开机自启 。

4.3.1.1 命令
//1-修改rc.local文件的配置
//rc.local是个文件
//在/etc/rc.d/rc.local文件的末尾,添加 /usr/local/nginx1.24.0/sbin/nginx此程序文件的配置//查看系统是否安装完整vim,如果正常安装肯定不止一行
rpm -qa|grep vim
//安装vim所有相关的包yum -y install vim*//2-修改rc.local文件
vim /etc/rc.d/rc.local//查看是否配置成功  cat /etc/rc.d/rc.local//3-赋予执行权限(必须运行这个命令来确保boot时,脚本能够被执行)
chmod +x /etc/rc.d/rc.local
4.3.1.2 截图

在这里插入图片描述
修改rc.local文件 :
vim /etc/rc.d/rc.local
在这里插入图片描述

查看是否配置成功 :
cat /etc/rc.d/rc.local

在这里插入图片描述
3-赋予执行权限(必须运行这个命令来确保boot时,脚本能够被执行)
目前所有用户均无执行权限。
在这里插入图片描述
chmod +x /etc/rc.d/rc.local 赋予执行权限:
在这里插入图片描述
赋予执行权限后,rc.local文件名称颜色会变成绿色,变深:
在这里插入图片描述
所有用户都拥有了执行权限:
在这里插入图片描述
重新启动服务器CentOS7.9 2009后,nginx已经可以开机自启,直接访问了,不需要再去执行./nginx 启动命令了:(此处在nginx中部署了前端的dist包)

//启动nginx
cd /usr/local/nginx1.24.0/sbin
./nginx 
或
./nginx -c  /usr/local/nginx1.24.0/conf/nginx.conf

在这里插入图片描述
查看nginx的进程 :

//查看nginx的进程
yum install net-tools -y
netstat -lntup | grep nginx//查看启动的nginx进程
ps -ef | grep nginxyum install lsof  
lsof -i :80

在这里插入图片描述

4.3.2 将dist包上传到指定目录/deploy/ruoyicloud_ui/中

参考我的这篇博客:[Windows11与CentOS7.9 2009下安装配置nginx后启动整个项目]
中的 4.4 部署dist包 。
在这里插入图片描述
查看/deploy/ruoyicloud_ui/dist的项目结构:
在这里插入图片描述
修改nginx.conf中的配置

配置nginx.conf
/usr/local/nginx1.24.0/conf
/usr/local/nginx1.24.0/conf/nginx.conf

文件内容太多,为了便于修改,以及避免错误,建议下载到Windows中配置,配置好再上传回去。

//命令修改
vim /usr/local/nginx1.24.0/conf/nginx.conf

修改nginx.conf中root html为root /deploy/ruoyicloud_ui/dist 访问打好的dist包,即我们刚刚上传的dist包所在的位置。

修改nginx.conf中的server_name localhost为server_name 192.168.1.16。

#user  nobody;
worker_processes  1;#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;#pid        logs/nginx.pid;events {worker_connections  1024;
}http {include       mime.types;default_type  application/octet-stream;#log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '#                  '$status $body_bytes_sent "$http_referer" '#                  '"$http_user_agent" "$http_x_forwarded_for"';#access_log  logs/access.log  main;sendfile        on;#tcp_nopush     on;#keepalive_timeout  0;keepalive_timeout  65;#gzip  on;server {listen       80;#server_name  localhost;server_name  192.168.1.16;#charset koi8-r;#access_log  logs/host.access.log  main;location / {#root   html;root  /deploy/ruoyicloud_ui/dist;index  index.html index.htm;}#error_page  404              /404.html;# redirect server error pages to the static page /50x.html#error_page   500 502 503 504  /50x.html;location = /50x.html {root   html;}# proxy the PHP scripts to Apache listening on 127.0.0.1:80##location ~ \.php$ {#    proxy_pass   http://127.0.0.1;#}# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000##location ~ \.php$ {#    root           html;#    fastcgi_pass   127.0.0.1:9000;#    fastcgi_index  index.php;#    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;#    include        fastcgi_params;#}# deny access to .htaccess files, if Apache's document root# concurs with nginx's one##location ~ /\.ht {#    deny  all;#}}# another virtual host using mix of IP-, name-, and port-based configuration##server {#    listen       8000;#    listen       somename:8080;#    server_name  somename  alias  another.alias;#    location / {#        root   html;#        index  index.html index.htm;#    }#}# HTTPS server##server {#    listen       443 ssl;#    server_name  localhost;#    ssl_certificate      cert.pem;#    ssl_certificate_key  cert.key;#    ssl_session_cache    shared:SSL:1m;#    ssl_session_timeout  5m;#    ssl_ciphers  HIGH:!aNULL:!MD5;#    ssl_prefer_server_ciphers  on;#    location / {#        root   html;#        index  index.html index.htm;#    }#}}

重新加载配置文件nginx.conf :
因为刚刚修改了配置文件nginx.conf(修改nginx.conf中root html为root /deploy/ruoyicloud_ui/dist 访问打好的dist包和修改nginx.conf中的server_name localhost为server_name 192.168.1.16。),所以要重新加载配置文件nginx.conf。

//检查Nginx配置是否正确,如果配置没有错误,将显示一条成功消息
cd /usr/local/nginx1.24.0/sbin
./nginx -t
或者/usr/local/nginx1.24.0/sbin/nginx -t//如果需要修改配置文件 ,修改之后要重新加载配置文件
cd /usr/local/nginx1.24.0/sbin
./nginx -s reload//启动nginx
cd /usr/local/nginx1.24.0/sbin
./nginx 
或
./nginx -c  /usr/local/nginx1.24.0/conf/nginx.conf#关闭nginx 
cd /usr/local/nginx1.24.0/sbin
./nginx -s stop # 停止

检查Nginx配置是否正确
//检查Nginx配置是否正确,如果配置没有错误,将显示一条成功消息
cd /usr/local/nginx1.24.0/sbin
./nginx -t
或者/usr/local/nginx1.24.0/sbin/nginx -t
在这里插入图片描述

到这一步,前端dist包就部署到了nginx中,nginx还设置了nginx开机自启,意味着打开服务器就可以直接访问到前端界面(地址:http://192.168.1.16:80)。

五、Nginx 服务器 SSL 证书安装部署(参考腾讯云官方文章)

参考:Nginx 服务器 SSL 证书安装部署
最近更新时间:2023-11-13 10:26:11 (现在是2023-11-18 19:05:37 ,5天前更新的)
参考的腾讯云文章链接中有视频进一步介绍在 Nginx 服务器安装 SSL 证书的操作过程。

5.1 证书安装

参考:Nginx 服务器 SSL 证书安装部署
本文档指导您如何在 Nginx 服务器中安装 SSL 证书。
说明:
本文档以证书名称 cloud.tencent.com 为例。
Nginx 版本以 nginx/1.18.0 为例。
当前服务器的操作系统为 CentOS 7,由于操作系统的版本不同,详细操作步骤略有区别。
安装 SSL 证书前,请您在 Nginx 服务器上开启 HTTPS 默认端口 443,避免证书安装后无法启用 HTTPS。具体可参考 服务器如何开启443端口?
SSL 证书文件上传至服务器方法可参考 如何将本地文件拷贝到云服务器。

SSL(Secure Sockets Layer 安全套接层),及其继任者传输层安全(Transport Layer Security,TLS)是为网络通信提供安全及数据完整性的一种安全协议,SSL端口号是443。TLS与SSL在传输层对网络连接进行加密。

第6步.编辑 Nginx 根目录下的 nginx.conf 文件。修改内容如下:

说明:如找不到以下内容,可以手动添加。可执行命令 nginx -t
,找到nginx的配置文件路径。 此操作可通过执行 vim /etc/nginx/nginx.conf 命令行编辑该文件。
由于版本问题,配置文件可能存在不同的写法。
例如:Nginx 版本为 nginx/1.15.0 以上请使用 listen 443 ssl 代替 listen 443 和 ssl on。

server {#SSL 默认访问端口号为 443listen 443 ssl; #请填写绑定证书的域名server_name cloud.tencent.com; #请填写证书文件的相对路径或绝对路径ssl_certificate cloud.tencent.com_bundle.crt; #请填写私钥文件的相对路径或绝对路径ssl_certificate_key cloud.tencent.com.key; ssl_session_timeout 5m;#请按照以下协议配置ssl_protocols TLSv1.2 TLSv1.3; #请按照以下套件配置,配置加密套件,写法遵循 openssl 标准。ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE; ssl_prefer_server_ciphers on;location / {#网站主页路径。此路径仅供参考,具体请您按照实际目录操作。#例如,您的网站主页在 Nginx 服务器的 /etc/www 目录下,则请修改 root 后面的 html 为 /etc/www。root html; index  index.html index.htm;}}

通过执行以下命令验证配置文件问题。若存在错误,重新配置或者根据提示修改存在问题。

nginx -t

通过执行以下命令重载 Nginx。重载成功,即可使用。

nginx -s reload

5.2 HTTP 自动跳转 HTTPS 的安全配置(可选)

如果您需要将 HTTP 请求自动重定向到 HTTPS。您可以通过以下操作设置:
1. 根据实际需求,选择以下配置方式:
在页面中添加 JS 脚本。
在后端程序中添加重定向。
通过 Web 服务器实现跳转。

Nginx 支持 rewrite 功能。若您在编译时没有去掉 pcre,您可在 HTTP 的 server 中增加 return 301 https://$host$request_uri;,即可将默认80端口的请求重定向为 HTTPS。
修改如下内容:

说明
1、未添加注释的配置语句,您按照下述配置即可。
2、由于版本问题,配置文件可能存在不同的写法。例如:Nginx 版本为
nginx/1.15.0 以上请使用 listen 443 ssl 代替 listen 443 和 ssl on。

server {#SSL 默认访问端口号为 443listen 443 ssl;#请填写绑定证书的域名server_name cloud.tencent.com; #请填写证书文件的相对路径或绝对路径ssl_certificate  cloud.tencent.com_bundle.crt; #请填写私钥文件的相对路径或绝对路径ssl_certificate_key cloud.tencent.com.key; ssl_session_timeout 5m;#请按照以下套件配置,配置加密套件,写法遵循 openssl 标准。ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;#请按照以下协议配置ssl_protocols TLSv1.2 TLSv1.3;ssl_prefer_server_ciphers on;location / {#网站主页路径。此路径仅供参考,具体请您按照实际目录操作。 #例如,您的网站主页在 Nginx 服务器的 /etc/www 目录下,则请修改 root 后面的 html 为 /etc/www。root html;index index.html index.htm;}
}
server {listen 80;#请填写绑定证书的域名server_name cloud.tencent.com; #把http的域名请求转成httpsreturn 301 https://$host$request_uri; 
}

通过执行以下命令验证配置文件问题。若存在错误,重新配置或者根据提示修改存在问题。

nginx -t

通过执行以下命令重载 Nginx。重载成功,即可使用。

nginx -s reload

如果浏览器地址栏显示安全锁标识,则说明证书安装成功。
在这里插入图片描述
如果网站访问异常,可参考以下常见问题解决方案进行处理:
无法使用 HTTPS 访问网站
部署 SSL 证书后,浏览器提示 “网站连接不安全”
访问站点提示连接不安全?
SSL 证书过期后重新申请部署依然提示 HTTPS 不安全?
在服务器上部署 SSL 证书后访问资源出现 404 报错

5.3 实操截图

六、参考

nginx官网
Nginx 服务器 SSL 证书安装部署
Nginx 服务器 SSL 证书安装部署-pdf
Windows11与CentOS7.9 2009下安装配置nginx后启动整个项目

我的相关博客:
打包命令根据package.json文件中的命令中的配置来写,下面的打包命令就是npm run build:prod,prod是production的缩写,意思是打生产环境的包,其中的 SET NODE_OPTIONS=--openssl-legacy-provider 是为了解决Node.js版本过高的问题(Error:0308010C:digital envelope routines::unsupported)而添加的。
详情见我这篇文章:
IDEA中Node.js环境下npm报错Error:0308010C:digital envelope routines:unsupported

npm相关安装配置等,参考我这篇文章:
安装配置nvm-windows对Node.js与npm进行版本控制

注意:yarn init或者npm init,执行输入信息后,会生成package.json文件。
参考我这篇文章:安装并配置使用包管理工具-Yarn

启动防火墙同时开放nginx80端口:
参考我这篇 查看CentOS版本及系统位数与设置CentOS 7.9 2009 防火墙配置放开端口的命令与过程

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:http://www.tpcf.cn/news/146959.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

测试开发环境下centos7.9下安装docker的minio

按照以下方法进行 1、安装docker,要是生产等还是要安装docker-ce yum install docker 2、启动docker service docker start 3、 查看docker信息 docker info 4、加到启动里 systemctl enable docker.service 5、开始docker pull minio/minio 但报错&#x…

【机器学习7】优化算法

1 有监督学习的损失函数 1.1 分类问题 对二分类问题, Y{1,−1}, 我们希望sign f(xi,θ)yi, 最自然的损失函数是0-1损失, 函数定义特点0-1损失函数非凸、非光滑,很难直接对该函数进行优化Hinge损失函数当fy≥1时&…

汽车以太网IOP测试新利器

IOP测试目的 汽车以太网物理层IOP(Interoperability )测试,即测试被测对象以太网物理层之间的互操作性。用于验证车载以太网PHY能否在有限时间内建立稳定的链路;此外,还用于验证车载以太网PHY可靠性相关的诊断特性&am…

Linux环境下C++ 接入OpenSSL

接上一篇:Windows环境下C 安装OpenSSL库 源码编译及使用(VS2019)_vs2019安装openssl_肥宝Fable的博客-CSDN博客 解决完本地windows环境,想赶紧在外网环境看看是否也正常。毕竟现在只是HelloWorld级别的,等东西多了&am…

linux时间调整

查看当前系统时间 [rootVM-12-12-centos ~]# date Sat Nov 18 16:09:11 CST 2023 Sat:表示星期六Saturday的缩写 Nov:表示十一月November的缩写 18:表示日期18号 16:09:11:时间 CST:China Standard Time中国标准…

认识Modbus通信协议(笔记)

Modbus 莫迪康 1979年 PLC 1969年 什么是modbus? 它是一个Bus,即总线协议。比如串口协议、IIC协议、SPI都是通信协议。 协议,顾名思义是一种规定和约束 Modbus协议是一种引用层的报文传输协议,RTU、ASCLL、TCP,都属于Modbus协…

文件哈希值,win和linux

示例: linux下 sha256sum filename openssl sha256 filename win下 certutil -hashfile filename SHA256。 其中 filename 为待测的文件名。 1、Windows 系统 certutil -hashfile filename MD5 certutil -hashfile filename SHA1 certutil -hashfile filename SHA2…

浅谈智能安全配电装置应用在银行配电系统中

【摘要】银行是国家重点安全保护部分,关系到社会资金的稳定,也是消防重点单位。消防安全是银行工作的重要组成部分。在银行配电系统中应用智能安全配电装置,可以提高银行的智能控制水平,有效预防电气火灾。 【关键词】银行&#…

如何快速下载mysql的不同版本并启动mysql服务?

如何快速下载mysql的不同版本并启动mysql服务? 下载mysql的安装版本 首先我们要使用到迅雷去下载,因为迅雷下载是很快的。在迅雷里面搜索下面的Mysql Installer安装窗口,如下图: 连接:https://dev.mysql.com/downlo…

计算矩阵边缘元素之和

Description 输入一个整数矩阵&#xff0c;计算位于矩阵边缘的元素之和。所谓矩阵边缘的元素&#xff0c;就是第一行和最后一行的元素以及第一列和最后一列的元素。 Input 第一行分别为矩阵的行数m和列数n&#xff08;m<100&#xff0c;n<100&#xff09;&#xff0c;…

fopen/fwrite/fread 对UNICODE字符写入的总结

windows对fopen函数进行了升级&#xff0c;可以支持指定文件的编码格式&#xff08;ccs参数指定&#xff09;。 例如&#xff1a; FILE *fp fopen("newfile.txt", "rt, ccsUTF-8"); 当以 ccs 模式打开文件时&#xff0c;进行读写操作的数据应为 UTF-16…

Trino 与Hive 有差异的函数

日常使用中发现trino和hive中的有一些函数存在差异&#xff0c;所以开此帖记录一下 这里只是记录trino和hive有差异的函数&#xff0c;遇到了就会记录一下&#xff0c;不定期更新 1. 查看集合中元素个数 hive&#xff1a;size() trino&#xff1a;cardinality() 2. map取值 …

迭代器与生成器

章节目录&#xff1a; 一、迭代器1.1 相关概述1.2 基本使用1.3 自定义迭代器 二、生成器2.1 相关概述2.2 基本使用2.3 三种应用场景 三、yield 和 class 定义的迭代器对比四、结束语 一、迭代器 1.1 相关概述 迭代是 Python 最强大的功能之一&#xff0c;是访问集合元素的一种…

Selenium自动化测试框架

一.Selenium概述 1.1 什么是框架? 框架&#xff08;framework&#xff09;是一个框子——指其约束性&#xff0c;也是一个架子——指其支撑性。是一个基本概念上的 结构用于去解决或者处理复杂的问题。 框架是整个或部分系统的可重用设计&#xff0c;表现为一组抽象构件及…

【Machine Learning in R - Next Generation • mlr3】

本篇主要介绍mlr3包的基本使用。 一个简单的机器学习流程在mlr3中可被分解为以下几个部分&#xff1a; 创建任务 比如回归、分裂、生存分析、降维、密度任务等等挑选学习器&#xff08;算法/模型&#xff09; 比如随机森林、决策树、SVM、KNN等等训练和预测 创建任务 本次示…

如何使用Java实现权限认证和登录jwt

这边可以关注我的博客 权限认证 轻松实现权限认证 登录拦截 轻松实现登录拦截 这样就可以了

C语言每日一题(32)环形链表

力扣网 141.环形链表 题目描述 给你一个链表的头节点 head &#xff0c;判断链表中是否有环。 如果链表中有某个节点&#xff0c;可以通过连续跟踪 next 指针再次到达&#xff0c;则链表中存在环。 为了表示给定链表中的环&#xff0c;评测系统内部使用整数 pos 来表示链表尾…

LLM大模型4位量化实战【GPTQ】

权重量化方面的最新进展使我们能够在消费类硬件上运行大量大型语言模型&#xff0c;例如 RTX 3090 GPU 上的 LLaMA-30B 模型。 这要归功于性能下降最小的新型 4 位量化技术&#xff0c;例如 GPTQ、GGML 和 NF4。 在上一篇文章中&#xff0c;我们介绍了简单的 8 位量化技术和出…

GZ038 物联网应用开发赛题第10套

2023年全国职业院校技能大赛 高职组 物联网应用开发 任 务 书 &#xff08;第10套卷&#xff09; 工位号&#xff1a;______________ 第一部分 竞赛须知 一、竞赛要求 1、正确使用工具&#xff0c;操作安全规范&#xff1b; 2、竞赛过程中如有异议&#xff0c;可向现场考…

Spring学习③__Bean管理

目录 IOC接口ApplicationContext 详解IOC操作Bean管理基于xml方式基于xml方式创建对象基于xml方式注入属性使用set方法进行注入通过有参数的构造进行注入p 名称空间注入&#xff08;了解&#xff09; 基于xml方式注入其他类型属性xml 注入数组类型属性 IOC接口 IOC思想基于IOC…