Nginx搭建与配置

本文详细介绍了在Linux环境下如何安装和配置Nginx,包括安装依赖如GCC、PCRE、ZLIB、OpenSSL,下载并编译Nginx源码,设置静态资源、反向代理及负载均衡,以及如何配置HTTPS,涉及SSL证书和端口开放。此外,还涵盖了Nginx常用命令及防火墙规则的调整。
摘要由CSDN通过智能技术生成

Nginx搭建

在线网址

linux下安装

1、安装gcc

安装 nginx 需要先将官网下载的源码进行编译,编译依赖 gcc 环境,如果没有 gcc 环境,则需要安装:
yum install gcc-c++

2、PCRE pcre-devel 安装

PCRE(Perl Compatible Regular Expressions) 是一个Perl库,包括 perl 兼容的正则表达式库。nginx 的 http 模块使用 pcre 来解析正则表达式,所以需要在 linux 上安装 pcre 库,pcre-devel 是使用 pcre 开发的一个二次开发库。nginx也需要此库。命令:
yum install -y pcre pcre-devel

3、zlib 安装

zlib 库提供了很多种压缩和解压缩的方式, nginx 使用 zlib 对 http 包的内容进行 gzip ,所以需要在 Centos 上安装 zlib 库。
yum install -y zlib zlib-devel

4、OpenSSL 安装

OpenSSL 是一个强大的安全套接字层密码库,囊括主要的密码算法、常用的密钥和证书封装管理功能及 SSL 协议,并提供丰富的应用程序供测试或其它目的使用。
nginx 不仅支持 http 协议,还支持 https(即在ssl协议上传输http),所以需要在 Centos 安装 OpenSSL 库。
yum install -y openssl openssl-devel

5、下载安装包

手动下载.tar.gz安装包,地址:https://nginx.org/en/download.html

6、解压

tar -zxvf nginx-1.18.0.tar.gz
cd nginx-1.18.0

7、配置

使用默认配置,在nginx根目录下执行
./configuremakemake install
查找安装路径: whereis nginx
Nginx常用命令
cd /usr/local/nginx/sbin/
./nginx 启动
./nginx -s stop 停止
./nginx -s quit 安全退出
./nginx -s reload 重新加载配置文件
ps aux|grep nginx 查看nginx进程

注意:如何连接不上,检查阿里云安全组是否开放端口,或者服务器防火墙是否开放端口!
相关命令:

开启

service firewalld start# 重启
service firewalld restart# 关闭
service firewalld stop# 查看防火墙规则
firewall-cmd --list-all# 查询端口是否开放
firewall-cmd --query-port=8080/tcp# 开放80端口
firewall-cmd --permanent --add-port=80/tcp# 移除端口
firewall-cmd --permanent --remove-port=8080/tcp
#重启防火墙(修改配置后要重启防火墙)
firewall-cmd --reload

参数解释1、firwall-cmd:是Linux提供的操作firewall的一个工具;2、–permanent:表示设置为持久;3、–add-port:标识添加的端口;

1.静态资源配置(项目)

在/usr/local/nginx文件下创建www目录(在此文件下存放页面或者项目)

location /train {
alias www/train_client/;
index index.html index.htm;
}

2.静态资源文件配置

[ 注意:服务器静态文件必须放在/data/front/static目录下,上述功能配置才生效。]
location /static/ {
root /data/front;
}

3.反向代理

location /api/ {
proxy_pass http://127.0.0.1:1256/;
}

4.反向代理(负载均衡,权重【weight数字越小代表访问次数越多】)

upstream lb{
server 127.0.0.1:8080 weight=1;
server 127.0.0.1:8081 weight=2;
}

location / {
proxy_pass http://lb;
}

5.配置https

配置 nginx 安装,加入fastdfs-nginx-module模块

这步很关键,我们需要切换到ngixn源码带有configure目录下执行,根据你们fastdfs-nginx-module相应地址灵活变更一下

./configure --prefix=/usr/local/nginx --with-http_ssl_module然后编译一下执行

报错之后查看
Nginx 安装 SSL 配置 HTTPS 超详细完整教程全过程-阿里云开发者社区 (aliyun.com)
使用 make 命令编译(使用make install会重新安装nginx),此时当前目录会出现 objs 文件夹

用新的 nginx 文件覆盖当前的 nginx 文件。

$ cp ./objs/nginx /usr/local/nginx/sbin/

再次查看安装的模块(configure arguments: –with-http_ssl_module说明ssl模块已安装)。

$ /usr/local/nginx/sbin/nginx -V

Nginx.conf配置

server {
listen 80;
location / {
rewrite ^(.*) https:// h t t p h o s t http_host httphostrequest_uri? permanent;
}
}

HTTPS server

#cert文件放在conf文件下
#cert表示配置nginx的证书
server {
listen 443 ssl;
server_name 127.0.0.1;
client_max_body_size 20M;
charset utf-8;
ssl_certificate cert/cert.pem;
ssl_certificate_key cert/cert.key;
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
server_tokens off;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;

#反向代理

location /api/ {
proxy_pass http://127.0.0.1:8010/;
}
#静态资源配置
location /static/ {
root /data/front;
}

}

感谢帮助以及阿里开发者社区

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值