1、下载文件
nginx下载地址 http://nginx.org/ http://nginx.org/en/download.html
2、安装基本工具
root@localhost ~]# yum install -y vim lrzsz gcc gcc-c++ make pcre pcre-devel zlib zlib-devel openssl openssl-devel
3、创建文件夹上传文件
[root@localhost ~]# mkdir /opt/soft
把下载好的nginx压缩包上传至 /opt/soft 路径下,并解压: “tar zxvf xxx.tar.gz”
4、进入nginx目录(本文实例Nginx-1.15.7)
[root@localhost ~]# cd /opt/soft/nginx-1.15.7
查看文件
5、编译文件
[root@localhost nginx-1.15.7]# ./configure
6、安装
[root@localhost nginx-1.15.7]# make && make install
7、查看已安装好的nginx
[root@localhost ~]# cd /usr/local/nginx/
[root@localhost ~]# ll /usr/local/nginx/
8、启动nginx
[root@localhost ~]# /usr/local/nginx/sbin/nginx
9、查看nginx的启动信息
[root@localhost ~]# ps -ef | grep nginx
10、关闭nginx
[root@localhost ~]# /usr/local/nginx/sbin/nginx -s quit
或
[root@localhost ~]# /usr/local/nginx/sbin/nginx -s stop
或
[root@localhost ~]# kill -9 13114 #(13114 是 PID)
[root@localhost ~]# kill -9 13115 #(13115 是 PID)
11、重新加载配置文件
[root@localhost ~]# /usr/local/nginx/sbin/nginx -s reload
12、启动时加载配置文件的路径:
[root@localhost ~]# /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf #(配置文件路径)
13、配置nginx
进入安装好的nginx下的conf目录
[root@localhost ~]# cd /usr/local/nginx/conf/
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
说明:此处的80即为该nginx的默认端口,可根据自己的需要进行修改
以下为配置Nginx的负责均衡
重新启动nginx,查看
14、关于nginx转发后获取不到客户端真实IP,需要在location里做如下配置
location / {
proxy_pass http://IP;
proxy_set_header Host $host;
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;
}
15、nginx 负载均衡配置https 网站,配置文件如下
网站配置完毕后,js、css 等文件找不到报错,需要在location中加一下映射
# HTTPS server
#
server {
listen 443 ssl;
listen 10001;
server_name localhost;
ssl on;
ssl_certificate /usr/local/nginx/conf/cert.crt;
ssl_certificate_key /usr/local/nginx/conf/cert.key;
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
location / {
root html;
index index.html index.htm;
proxy_redirect off;
proxy_set_header Host $host:10001;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_next_upstream error timeout invalid_header http_500 http_503 http_404;
proxy_max_temp_file_size 128m;
proxy_pass https://iosapi;
}
location ~ .*\.(html|htm|gif|jpg|jpeg|bmp|png|ico|txt|js|css)$ {
proxy_pass https://iosapi;
}
}
如图:
16、网站更换CA证书或升级证书 导致nginx转发报错:
[error] 9126#0: *1791 SSL_do_handshake() failed (SSL: error:14077410:SSL routines:SSL23_GET_SERVER_HELLO:sslv3 alert handshake failure) while SSL handshaking to upstream, client: ***, server: localhost, request: "POST /smc/leadership HTTP/1.1", upstream: "https://***:50044/smc/leadership", host: "www.***.com:50043", referrer: "http://****:8002/Home/Index"
解决方案:
在nginx配置文件中的location里加入下面代码:
proxy_ssl_server_name on;
proxy_ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
重启nginx服务后即可生效