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服務後即可生效
本文參考