Nginx 安装及配置、负载均衡https网站及转发后页面js、css等路径找不到问题、更换证书导致问题解决

官网下载nginx:http://nginx.org/en/download.html

安装nginx编译环境:yum install -y gcc-c++

安装pcre库解析正则:yum install -y pcre pcre-devel

安装zlib库用于压缩解压缩:yum install -y zlib zlib-devel

安装openssl库:yum install -y openssl openssl-devel

cd 到nginx解压目录编译(指定目录及支持ssl https):./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module

编译完成后安装:make && make install 

运行nginx:cd /usr/local/nginx/sbin/                  /.nginx

 配置conf 文件:

结束掉nginx:./nginx -s quit或者./nginx -s stop 或者ps aux |grep nginx (查出nginx进程)  kill -quit 进程主ID  (也可以-9强制结束两个进程)

 重启nginx:sbin 目录下  ./nginx  

nginx 负载均衡配置成功。

【重新加载配置文件:./nginx -s reload  启动时加载配置文件的路径:sbin/nginx -c conf/nginx.conf】

 关于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;
    }

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;
        }
    }

如图:

网站更换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服务即可。

原文地址:https://www.cnblogs.com/MrZheng/p/8523292.html