一、安装
1)添加源
sudo rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
2)安装Nginx
sudo yum install -y nginx
3)启动Nginx并设置开机自动运行
sudo systemctl start nginx.service
sudo systemctl enable nginx.service
二、配置及使用
1.配置域名映射
于/etc/nginx/conf.d目录下,增加配置文件www-dev.integration.enjoyor.net.conf(建议配置文件以域名命名,且一个配置文件配置一个域名)
upstream integration {
server 192.168.20.106:9980 weight=1 max_fails=1 fail_timeout=1s;
}
server {
listen 80;
server_name www-dev.integration.enjoyor.net;
#charset koi8-r;
access_log /var/log/nginx/logs/dev.integration.enjoyor.net.access.log main;
error_log /var/log/nginx/logs/dev.integration.enjoyor.net.error.log notice;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
proxy_next_upstream http_500 http_502 http_503 http_504 timeout error invalid_header;
proxy_set_header Host $host;
proxy_cookie_path /integrationweb/ /;
proxy_set_header Referer $http_referer;
proxy_set_header Cookie $http_cookie;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://integration/integrationweb/;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
重点
1.配置
proxy_pass http://integration/integrationweb/;
upstream name / tomcat war包名
2.权限
手动创建 日志目录/var/log/nginx/logs ,nginx没有权限
2.报错与解决
1)权限问题
Nginx * 1 connect() to 192.168.20.106:9980 failed (13: Permission denied)
此处产生问题的原因是:以root用户的身份操作nginx。从服务器运维的角度,每一个工具都应该创建其对应的用户,只有该用户有权限操作,若都以root权限操作会留下安全隐患(攻击者可从nginx日志获取root信息,进而操作服务器)。
但此教程作为测试便不多做限制,快速达成效果即可。
解决方案如下:
更改nginx用户
/etc/nginx/目录下 nginx.conf 文件 user改为root
user root;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
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 /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
}
2)网络问题
解决方案如下:
解决方法:
一、关闭SeLinux
查看SELinux状态:
1、/usr/sbin/sestatus -v ##如果SELinux status参数为enabled即为开启状态
SELinux status: enabled
2、getenforce ##也可以用这个命令检查
关闭SELinux:
1、临时关闭(不用重启机器):
setenforce 0 ##设置SELinux 成为permissive模式
##setenforce 1 设置SELinux 成为enforcing模式
2、修改配置文件需要重启机器:
修改/etc/selinux/config 文件
将SELINUX=enforcing改为SELINUX=disabled
重启机器即可
二、执行下面的命令
setsebool -P httpd_can_network_connect 1
三、hosts增加DNS解析(用于生产环境)
c:\Windows\System32\Drivers\etc 路径下 hosts文件增加192.168.20.90(具体DNS服务器)
会优先以192.168.20.90 配置的 www-dev.integration.enjoyor.net 指向 192.168.20.106的规则解析域名
cmd 中 ipconfig /flushdns 刷新DNS缓存后生效。
四、hosts添加本地映射(用于开发环境)
1.修改hosts文件,让域名以本地配置的规则进行映射
# localhost name resolution is handled within DNS itself.
#192.168.20.90
127.0.0.1 www-dev.integration.enjoyor.net
2.安装本地nginx
1)nginx.conf配置
文件末尾添加 include conf.d/*.conf,引入同级文件夹conf.d下的所有.conf文件
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
include conf.d/*.conf;
}
2)conf.d配置
www-dev.integration.enjoyor.net.conf 文件映射本地tomcat
upstream integration {
server 127.0.0.1:8081 weight=1 max_fails=1 fail_timeout=1s;
}
server {
listen 80;
server_name www-dev.integration.enjoyor.net;
location / {
proxy_next_upstream http_500 http_502 http_503 http_504 timeout error invalid_header;
proxy_set_header Host $host;
proxy_cookie_path /integrationweb/ /;
proxy_set_header Referer $http_referer;
proxy_set_header Cookie $http_cookie;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://integration/integrationweb/;
}
}
至此,通过修改hosts文件的域名映射关系,可以在本地灵活切换开发和生产环境的接口,便于后端人员调试。