- 添加nginx到yum源
rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
- 安装Nginx
yum install -y nginx
- 启动Nginx
systemctl start nginx
- CentOS 7 开机启动Nginx
systemctl enable nginx
- Nginx配置信息
网站文件存放默认目录
/usr/share/nginx/html
网站默认站点配置
/etc/nginx/conf.d/default.conf
自定义Nginx站点配置文件存放目录
/etc/nginx/conf.d/
Nginx全局配置
/etc/nginx/nginx.conf
- 将NGINX加到SELinux的允许名单
如果nginx反向代理本机服务,遇到502错误,可以看看是不是SELinux造成的。
查看SELinux状态:
/usr/sbin/sestatus -v ##如果SELinux status参数为enabled即为开启状态
SELinux status: enabled
getenforce ##也可以用这个命令检查
临时关闭/开启(不用重启机器):
setenforce 0 ##设置SELinux 成为permissive模式
setenforce 1 ##设置SELinux 成为enforcing模式
如果关闭SELinux后,反向代理成功的话,就可以判断是SELinux造成的。
把NGINX加入SELinux的允许名单的方法
yum install policycoreutils-python
cat /var/log/audit/audit.log | grep nginx | grep denied | audit2allow -M mynginx
semodule -i mynginx.pp
- 配置示例
client_max_body_size 500M;
client_body_buffer_size 1024k;
fastcgi_buffer_size 128k;
fastcgi_buffers 256 16k;
upstream websocket {
server 192.168.2.106:8280;
}
upstream eber {
server 192.168.2.105:8081 max_fails=1 fail_timeout=30s;
server 192.168.2.106:8081;
}
server {
location /websocket {
proxy_pass http://websocket/websocket;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
location /sockjs/websocket {
proxy_pass http://websocket/sockjs/websocket;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
location / {
proxy_pass http://eber;
proxy_connect_timeout 3s; #默认值60s, nginx连接到后端服务器的连接超时时间
}
}