CentOS7安装及配置Nginx
参考:
Centos 7安装nginx
搭建Nginx图片服务器(Linux)
Nginx配置后图片访问 404问题解决
1. 安装Nginx
- 安装nginx环境
- gcc
# nginx是C语言开发,编译依赖gcc环境,如果没有gcc环境,需要安装gcc。下载官网nginx源码并编译
yum install gcc-c++
- PCRE
# pcre-devel是使用pcre开发的一个二次开发库。nginx也需要此库
yum install -y pcre pcre-devel
- zlib
# zlib库提供了很多种压缩和解压缩的方式,nginx使用zlib对http包的内容进行gzip,所以需要在linux上安装zlib库
yum install -y zlib zlib-devel
- openssl
# OpenSSL 是一个强大的安全套接字层密码库,囊括主要的密码算法、常用的密钥和证书封装管理功能及SSL协议,并提供丰富的应用程序供测试或其它目的使用。nginx不仅支持http协议,还支持https(即在ssl协议上传输http),所以需要在linux安装openssl库
yum install -y openssl openssl-devel
- 下载nginx源
地址:http://nginx.org/en/download.html
- 将下载后的nginx源文件上传到Linux
# 笔者将nginx上传到/usr/server/tools,如果没有该文件夹,可先新建
mkdir /usr/server/tools
# 解压缩
tar -zxvf nginx-1.9.1.tar.gz
# 切换到解压缩后的文件夹
cd nginx-1.9.1
- 配置参数(切换到解压后的nginx目录下)
# 打开linux终端工具,输入如下指令
./configure \
--prefix=/usr/local/nginx \
--pid-path=/var/run/nginx/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--with-http_gzip_static_module \
--http-client-body-temp-path=/var/temp/nginx/client \
--http-proxy-temp-path=/var/temp/nginx/proxy \
--http-fastcgi-temp-path=/var/temp/nginx/fastcgi \
--http-uwsgi-temp-path=/var/temp/nginx/uwsgi \
--http-scgi-temp-path=/var/temp/nginx/scgi
- make安装
make & make install
2. 测试Nginx
- 错误解决
- [emerg] mkdir() “/var/temp/nginx/client” failed (2: No such file or directory)
# 解决方案
mkdir -p /var/temp/nginx/client
- [emerg] open() “/var/run/nginx/nginx.pid” failed (2: No such file or directory)
# 因为每次启动,var/run/nginx这个文件夹都会被删除
# 解决方案
vim /usr/local/nginx/conf/nginx.conf
# 去掉下面的注释符号
#pid logs/nginx.pid
# 在/usr/local/nginx/下新建logs文件夹
mkdir /usr/local/nginx/logs
# 切换到安装位置,也就是之前配置configure的--prefix
cd /usr/local/nginx/sbin
#启动,nginx.conf是nginx的默认配置文件,也可以使用 nginx -c 指定配置文件启动sbin/nginx
nginx [-c 配置文件全路径]
# 查看nginx进程(master进程和worker进程),结果如下图1
ps -ef | grep nginx
-
启动Nginx
在浏览器输入ip即可(若无法访问,可能是防火墙没有关闭,指令:systemctl stop firewalld.service) -
Nginx开关命令
# 停止nginx
## 1 快速停止:此方式相当于先查出nginx进程id再使用kill命令强制杀掉进程
cd /usr/local/nginx/sbin
./nginx -s stop
## 2 完整停止(建议使用):此方式停止步骤是待nginx进程处理任务完毕进行停止
cd /usr/local/nginx/sbin
./nginx -s quit
# 重启nginx
## 1 先停止再启动(建议使用): 对nginx进行重启相当于先停止nginx再启动nginx,即先执行停止命令再执行启动命令
./nginx -s quit
./nginx
## 2 重新加载配置文件:nginx的配置文件nginx.conf修改后,配置生效需要重启nginx,使用-s reload不用先停止
3. Nginx反向代理配置
- 打开nginx配置文件:nginx/conf/nginx.conf
# 拦截url:/proxy
location /proxy {
# 将业务代理到指定端口
proxy_pass http://ip:port/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
- 测试反向代理
# 浏览器输入部署Nginx服务器器的ip地址;如:192.168.1.1,跳转到proxy所配置的进程表明成功
4. Nginx负载均衡配置
- 打开nginx配置文件:nginx/conf/nginx.conf
# 配置代理server组,标识为myWeb
upstream myWeb {
# 采用权重的方式进行负载均衡,包含有两台服务器
server ip1:port1 weight=10;
server ip2:port2 weight=10;
}
server {
listen 80;
server_name localhost;
location / {
# 将请求代理到所配置的主机上
proxy_pass http://myWeb/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
- 测试负载均衡
# 浏览器输入部署Nginx服务器器的ip地址;如:192.168.1.1,每次刷新,Nginx会将请求转发到其下的服务器下
5. 搭建Nginx图片服务器
- 打开nginx配置文件:nginx/conf/nginx.conf
location /images/ {
# 映射的文件夹路径
root /usr/server/
# 浏览器可以预览
autoindex on;
}
- 注意
上述配置后,浏览器实际访问的地址为:http://ip:port/usr/server/images/xxx
所以root配置为图片文件夹的上一层目录即可