安装nginx
版本: nginx-1.12.1 稳定版
系统: centos 6.5
一、准备事项
(1) 因为nginx需要访问80端口所以请先关闭或者开放防火墙端口,和selinux。
参考命令
关闭防火墙:
[root@local ~]# iptables -I INPUT -p tcp --dport 80 -j ACCEPT
[root@local ~]# service iptables save
关闭selinux:
[root@local ~]# setenforce 0
[root@local ~]# vim /etc/selinux/config
将SELINUX=enforcing改为SELINUX=disabled
二,安装
1、环境准备:先安装准备环境
yum install gcc gcc-c++ automake pcre pcre-devel zlip zlib-devel openssl openssl-devel
2、下载nginx 安装包: 官网地址:http://nginx.org/
3、解压安装包:
4、编译nginx:make
[root@Server1 nginx-1.8.1]# ./configure --prefix=/usr/local/nginx --sbin-path=/usr/local/nginx/sbin/nginx --conf-path=/usr/local/nginx/conf/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx/nginx.pid --lock-path=/var/lock/nginx.lock --user=nginx --group=nginx --with-http_ssl_module --with-http_stub_status_module --with-http_gzip_static_module --http-client-body-temp-path=/var/tmp/nginx/client/ --http-proxy-temp-path=/var/tmp/nginx/proxy/ --http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi --http-scgi-temp-path=/var/tmp/nginx/scgi --with-pcre
5、生成脚本及配置文件:make
6、安装:make install
7、启动:
将监听端口改为8090,避免80端口冲突:
三,注意
Ubuntu安装时
解决依赖包openssl安装,命令:
[cpp] view plain copy
1. sudo apt-get install openssl libssl-dev
解决依赖包pcre安装,命令:
[cpp] view plain copy
1. sudo apt-get install libpcre3 libpcre3-dev
解决依赖包zlib安装,命令:
[cpp] view plain copy
1. sudo apt-get install zlib1g-dev
nginx负载均衡简单配置
准备三台虚拟机来做这个实验:
192.168.232.132 web服务器
192.168.232.133 web服务器
192.168.232.134 负载均衡服务器
1.yum安装nginx
yum install nginx
2.启动nginx
chkconfig nginx on
service nginx start
3.配置负载均衡服务器
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
upstream test.miaohr.com {
server 192.168.232.132:80;
server 192.168.232.133:80;
}
server {
listen 80;
server_name test.miaohr.com;
charset utf-8;
location / {
root html;
index index.html index.htm;
proxy_pass http://test.miaohr.com;
proxy_set_header X-Real-IP $remote_addr;
client_max_body_size 100m;
}
location ~ ^/(WEB-INF)/ {
deny all;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /var/www/html/;
}
}
}
Nginx文件压缩
图片压缩基本没效果