最近继续整理Linux相关文档。这次整理的是Nginx,这里将自己整理的详细文档做个笔记。
1. 安装环境依赖包
1、 gcc 语言编译器套件。
2、 pcre 兼容正则表达式的库 rewrite 模块需要。
3、 zlib 提供数据压缩函数库 例如gzip压缩。
4、 openssl 使用https所需的ssl。
一起安装四个依赖环境包 (如果某些组件已安装可以不用安装)
yum -y install gcc zlib zlib-devel pcre-devel openssl openssl-devel
2. 下载和解压安装包
官网查找最新安装包。
http://nginx.org/en/download.html
下载1.17.1下载地址
http://nginx.org/download/nginx-1.17.1.tar.gz
在/usr/local下创建Nginx目录。 (目录可以自定义)
mkdir Nginx
下载
wget http://nginx.org/download/nginx-1.17.1.tar.gz
解压
tar -xvf Nginx-1.17.1.tar.gz
3. 安装
切换到安装后的目录。
cd Nginx-1.17.1
./configure
make
make install
这几步如果有报错,多数是因为依赖环境没装好比如gcc等,需要重新安装再重复此步骤。
添加到环境变量
ln -s /usr/local/Nginx/sbin/Nginx /usr/bin
安装成功后查看版本
Nginx -v
4. 设置开机启动
vim /lib/systemd/system/Nginx.service
注意Nginx 路径必须为自己安装的路径
【以下纯文本可以复制】
[Unit] Description=Nginx - high performance web server Documentation=http://Nginx.org/en/docs/ After=network.target [Service] Type=forking ExecStart=/usr/local/Nginx/sbin/Nginx ExecReload=/usr/local/Nginx/sbin/Nginx -s reload ExecStop=/usr/local/Nginx/sbin/Nginx -s stop PrivateTmp=true [Install] WantedBy=multi-user.target
开机启动
5. Nginx使用与配置
常用命令
Nginx # 运行Nginx Nginx -s reload # 重新载入配置文件并运行 Nginx -s reopen # 重启 Nginx Nginx -s stop # 停止 Nginx
运行Nginx
配置文件
位置(注意自己安装的目录)
vim /usr/local/Nginx/conf/Nginx.conf
启动Nginx后 可以直接通过http://localhost (或者http://自己的ip)访问。查看Nginx欢迎页面。
如果服务器80端口被占用了 那么使用Nginx命令时会报错。请修改配置文件里的默认80端口即可。
配置文件的修改后必须要:Nginx –s reload 才能生效。
静态服务器
server { listen 80; #监听端口 server_name localhost; #如果绑定了域名 这里填写具体域名 client_max_body_size 1024M; #客户端最大上传文件限制 location / { autoindex on; //开启目录访问 root /data/wwwroot/webapp; #站点目录 index index.html; #首页 } }
动静分离
这里展示通过扩展名分离的方法
当然还有通过请求分离使用在localtion /static/ {} 等。
server { listen 80; #监听端口 server_name localhost; #如果绑定了域名 这里填写具体域名 #静态数据 location ~ .(gif|jpg|jpeg|png|bmp|swf|css|js)$ { root /data/wwwroot/webapp/html; } #动态请求 location ~ .(aspx|cshtml)$ { proxy_pass http://localhost:8080 #动态服务器站点运行地址 } }
反向代理
适合单台服务器应用程序部署,转发
server { listen 80; server_name localhost; client_max_body_size 1024M; location / { proxy_pass http://localhost:8080; #代理服务器 比如动态应用程序站点 proxy_set_header Host $host:$server_port; #请求头信息部分信息一并转发到代理服务器 } }
均衡负载
最常用的,适合多台服务器部署应用程序,对外都是同一个域名或站点访问
# 服务器列表 upstream webapp{ server 192.1681.2:8080 weight=9; #weight 权重 server 192.168.1.3:8080 weight=1; } server { listen 81; server_name localhost; client_max_body_size 1024M; location / { proxy_pass http://webapp; #代理指向服务器列表 proxy_set_header Host $host:$server_port; #获取真实客户端访问IP,原理还是将客户端和IP有关的请求头转发到应用服务器 proxy_set_header X-Real-IP $remote_addr; proxy_set_header REMOTE-HOST $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } }
配置Https
https需要依赖openssl包。
Nginx开启SSL模块
检查自己是否有开启SSL模块
Nginx –V (大写的V)
看到有with-http_ssl_module证明已经开启。
如果没有则证明没有开启;以下操作都是针对没有开启with-http_ssl_module的。
进入自己的安装目录执行:
./configure --prefix=/usr/local/Nginx --with-http_stub_status_module --with-http_ssl_module
配置完成后执行编译
make
备份已经安装好的Nginx (注意自己的安装目录)
cp /usr/local/Nginx/sbin/Nginx /usr/local/Nginx/sbin/Nginx.bak
停止正在运行的Nginx
Nginx -s stop
cp ./objs/Nginx /usr/local/Nginx/sbin/
查看是否配置成功
Nginx –V (大写的V)
#server { listen 443 ssl; #监听端口改为443 server_name localhost; ssl_certificate cert.pem; #证书prm文件路径 ssl_certificate_key cert.key; #证书key文件路径 ssl_session_cache shared:SSL:1m; #设置会话缓存大小 ssl_session_timeout 5m; #客户端可以重用会话缓存中ssl参数的过期时间 ssl_ciphers HIGH:!aNULL:!MD5; #加密方式 ssl_prefer_server_ciphers on; #设置加密算法时,优先使用服务端的加密算法 location / { root html; index index.html index.htm; } }
配置Http2
配置Http2 Nginx 版本必须大于1.10.0以上。Openssl版本必须大于1.0.2。
在Nginx里面使用Http2必须得使用Https才行。
可以通过Nginx –V查看目前已安装的版本。
Http2需要开启with-http_v2_module模块;
配置with-http_v2_module等模块然后make 然后覆盖安装等。 具体不就不再演示了和配置Https一样。
./configure --prefix=/usr/share/Nginx --with-http_stub_status_module --with-http_ssl_module --with-http_v2_module --with-http_gzip_static_module --with-ipv6 --with-http_sub_module
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。