编译安装 Nginx 及简单配置

1、安装依赖包

yum -y install pcre-devel zlib-devel openssl-devel gcc gcc-c++ make    //pcre,openssl 可选择编译安装

2、创建应用用户

useradd -M -s /sbin/nologin nginx

3、安装 nginx

#tar xf nginx-1.14.0.tar.gz -C /usr/local/src/
#cd /usr/local/src/nginx-1.14.0/
#./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_stub_status_module \
--with-http_ssl_module \
--without-http_rewrite_module \
--with-http_gzip_static_module \
--with-pcre=/home/ap/appuser/web_server/Package/pcre-8.41 \
--with-openssl=/home/ap/appuser/web_server/Package/openssl-1.0.2h \

注释:
--without-http_rewrite_module //重写模块默认开
--with-http_gzip_static_module  //开启gzip静态模块,用于发送预压缩的文件
--with-http_ssl_module  //用于支持HTTPS

4、nginx 启动、停止

#/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf  //指定配置文件启动
#/usr/local/nginx/sbin/nginx -s reload //平滑重启
#kill -HUP nginx主进程号(cat /usr/local/nginx/logs/nginx.pid)  //平滑重启
#/usr/local/nginx/sbin/nginx -s stop  //快速停止
#/usr/local/nginx/sbin/nginx -s quit  //不接收新的请求,等连接的请求完成在停止(生产建议使用此方法)
#/usr/local/nginx/sbin/nginx -t  //验证nginx配置文件是否正确

5、nginx 代理

    server {
        listen       8080;
        server_name  localhost;

        location / {
            root   html;
            index  index.html index.htm;
        }
        
        location /web {  prox_pass http://127.0.0.1:8080/web;  }
        location /www {  prox_pass http://127.0.0.1:8080/web;  }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }

6、nginx 负载均衡

    upstream java_server {     
        
        server 192.168.3.11:8080;
        
        server 192.168.3.12:8080;
    }
    
    server {
        listen       8080;
        server_name  localhost;

    location / { 
            root  html; 
            index  index.html index.htm; 
            proxy_pass http://java_server; 
        }
    }

7、nginx 证书配置

    server {
        listen       443 ssl;
        server_name  localhost;

        ssl_certificate      ssl/server.cer; //公钥证书(注意证书路径,我的证书是在nginx/conf/ssl/下)
        ssl_certificate_key  ssl/server.key;  //私钥证书

        ssl_session_cache    shared:SSL:10m;
        ssl_session_timeout  10m;

        ssl_ciphers  HIGH:!aNULL:!MD5;
        ssl_prefer_server_ciphers  on;

        location / {
            root   html;
            index  index.html index.htm;
        }
    }

猜你喜欢

转载自www.cnblogs.com/yuxl/p/11433864.html