一,Linux 6,7 下的安装依赖
yum -y install pcre-devel openssl openssl-devel libssl-dev gcc-c++
二,安装 pcre,让 Nginx 支持 Rewrite 功能
0,pcre 地址:https://sourceforge.net/projects/pcre/
1,下载:wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.44.tar.gz
2,解压:tar zxvf pcre-8.44.tar.gz
3,进入:cd pcre-8.44
4,配置:./configure
5,编译:make
6,安装:make install
7,验证:pcre-config --version,会出现:8.44,表示安装成功!
三,安装 Nginx
1,下载:wget http://nginx.org/download/nginx-1.16.1.tar.gz
2,解压:tar zxvf nginx-1.16.1.tar.gz
3,进入:cd nginx-1.16.1
4,配置:./configure(可以加上:--with-http_stub_status_module --with-http_ssl_module --with-pcre=/usr/local/src/pcre-8.44)
5,编译:make
6,安装:make install
7,验证:/usr/local/nginx/sbin/nginx -t,出现:nginx: ... syntax is ok ... test is successful ,表示安装成功!
四,CentOS 7 安装 Nginx
1,添加 yum 源:rpm -ivh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
2,安装:yum -y install nginx
3,启动:systemctl start nginx
4,开机自启动:systemctl enable nginx
5,重启服务:systemctl restart nginx
6,重新加载配置:systemctl reload nginx
7,配置文件:配置文件:/etc/nginx/nginx.conf
8,默认配置文件:/etc/nginx/conf.d/default.conf
五,配置
1,启动服务:/usr/local/nginx/sbin/nginx
2,关闭服务:/usr/local/nginx/sbin/nginx -s stop
3,重启服务:/usr/local/nginx/sbin/nginx -s reload
4,加入系统服务:vim /etc/rc.local,添加:/usr/local/nginx/sbin/nginx
5,在 /usr/local/nginx/conf 中创建:gzip.conf
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_types text/plain text/css application/x-javascript;
output_buffers 1 64k;
postpone_output 1460;
6,在 /usr/local/nginx/conf 中创建:proxy.conf
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size 10m; #文件上传大小限制
client_body_buffer_size 256k;
proxy_connect_timeout 300;
proxy_send_timeout 300;
proxy_read_timeout 300;
proxy_buffer_size 16k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
六,最精简的 nginx.conf
七,Nginx 负载均衡配置:
upstream test {
server localhost:8080;
server localhost:8081;
}
server {
listen 80;
server_name localhost;
client_max_body_size 1024M;
location / {
proxy_pass http://test;
proxy_set_header Host $host:$server_port;
}
}