新手小白需要掌握的Nginx部署技能
前言
大家好,我是testerzhang,今天给大家分享一个Nginx编译部署的文章。其实之前我也写过一篇,但是有些东西更新了,还是重新在写一篇分享给大家吧。
1.Nginx
维基百科的定义:
Nginx(发音同"engine X")是异步框架的网页服务器,也可以用作反向代理、负载平衡器和HTTP缓存。该软件由伊戈尔·赛索耶夫(Игорь Сысоев)创建并于2004年首次公开发布。2011年成立同名公司以提供支持。2019年3月11日,Nginx公司被F5 Networks以6.7亿美元收购。
Nginx是免费的开源软件,根据类BSD许可证的条款发布。一大部分Web服务器使用Nginx,通常作为负载均衡器。
2.系统依赖库安装
请使用root用户安装Nginx所需要的依赖库
# yum install -y gcc gcc-c++ pcre-devel openssl-devel
3.下载Nginx
$ mkdir bm$ cd bm$ wget http://nginx.org/download/nginx-1.20.1.tar.gz
4.编译Nginx
•解压编译
$ tar zxf nginx-1.20.1.tar.gz$ cd nginx-1.20.1/$ ./configure --prefix=/home/testerzhang/nginx --with-http_stub_status_module --with-http_ssl_module
如果你需要其他模块,比如http_v2_module模块
$ ./configure --prefix=/home/testerzhang/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_v2_module
•编译安装
$ make$ make install
这时候我们就可以在安装目录查看到编译好的文件了。
•删掉安装目录的html文件夹下的文件
$ cd /home/testerzhang/nginx/html$ rm *.html
5.打包编译好的Nginx目录
这里打包最原始的配置,当然如果你要提前修改Nginx配置文件,再打包也是可以的。
$ cd ~/$ tar zcf nginx.tar.gz nginx
然后你就可以拷贝到你想要部署的机器。
6.新机器部署
•在你想要部署的目录,解压压缩包
$ tar zxf nginx.tar.gz$ cd nginx
•修改配置文件
$ vim conf/nginx.conf
这里列举下需要修改的常规配置
# 由于部署的机器不是按照编译打包的账号来的,所以这里要配置实际部署机器的绝对路径。error_log /opt/testerzhang/nginx/logs/error.log;http {...# 设置访问日志信息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 /opt/testerzhang/nginx/logs/access.log main;# 隐藏nginx版本server_tokens off;# 隐藏后端服务信息proxy_hide_header Server;...# 这里设置端口80的端口配置,并自动重定向到https的网站地址。server {listen 80 ;server_name www.xxx.top;access_log /opt/testerzhang/nginx/logs/80.access.log main;# 限制IP访问,前提对应配置的文件要存在#include blockip.conf;return 301 https://www.xxx.top;}server {listen 443 ssl ;server_name www.qahome.top;access_log /opt/testerzhang/nginx/logs/443.access.log main;# 限制IP访问,前提对应配置的文件要存在#include blockip.conf;# 添加同源策略,防止跨站脚本,禁止某些浏览器的内容类型探嗅add_header X-Frame-Options "SAMEORIGIN";add_header X-XSS-Protection "1; mode=block";add_header X-Content-Type-Options "nosniff";# SSL证书的配置ssl_certificate /opt/testerzhang/zhengshu/1_www.xxx.top_bundle.crt;ssl_certificate_key /opt/testerzhang/zhengshu/2_www.xxx.top.key;ssl_protocols TLSv1.2;ssl_session_cache shared:SSL:1m;ssl_session_timeout 5m;ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;ssl_prefer_server_ciphers on;# 根目录对应的静态文件目录location / {root /opt/testerzhang/website;index index.html index.htm;}# 转发后端请求#location /backend {# proxy_set_header Host $host:$proxy_port;# proxy_set_header X-Real-IP $remote_addr;# proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;## proxy_pass http://10.10.10.10:58080;#}...}}
7.启停命令
正常在编译后的安装目录,不需要指定配置文件,但是由于是部署新机器,可能不是同一个账号,你需要加上绝对路径的配置文件
•启动
$ ./nginx -c /opt/testerzhang/nginx/conf/nginx.conf
•停止
$ ./nginx -c /opt/testerzhang/nginx/conf/nginx.conf -s stop
•重新加载Nginx配置
$ ./nginx -c /opt/testerzhang/nginx/conf/nginx.conf -s reload
文末
如果你喜欢我的推荐,可以点击"收藏"进行稍后阅读,可以点击"在看"支持下,也可以分享下此文章给你的其他朋友。
testerzhang
十年测试生涯,爱分享技术。头条号:testerzhang
Official Account
推荐阅读
••••••
