http://zfl110.iteye.com/blog/1155149
原址:
安装Nginx 1.首先安装pcre-8.02.tar 否则 执行完后会提示一个错误,说缺少PCRE library 这个是HTTP Rewrite 模块,也即是url静态化的包 可上传pcre-8.02.tar.gz,输入如下命令安装:- tar xzvf pcre-8.02.tar
- ./configure
- make
- make install
2.执行如下命令解压nginx:
- tar xzvf nginx-0.8.35.tar.gz
3.编译安装nginx
- cd nginx-0.8.35
- ./configure --with-http_stub_status_module --with-http_ssl_module --with-http_sub_module
#启动server状态页和https模块
- --with-http_stub_status_module 必须加上,不然报unknown directive "stub_status"
- make
- make install
4.nginx安装成功后的安装目录为/usr/local/nginx 在conf文件夹中新建proxy.conf,用于配置一些代理参数,内容如下:
- #!nginx (-)
- # proxy.conf
- proxy_redirect off;
- proxy_set_header Host $host;
- proxy_set_header X-Real-IP $remote_addr; #获取真实ip
- #proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; #获取代理者的真实ip
- client_max_body_size 10m;
- client_body_buffer_size 128k;
- proxy_connect_timeout 90;
- proxy_send_timeout 90;
- proxy_read_timeout 90;
- proxy_buffer_size 4k;
- proxy_buffers 4 32k;
- proxy_busy_buffers_size 64k;
- proxy_temp_file_write_size 64k;
编辑安装目录下conf文件夹中的nginx.conf,输入如下内容:
- #--------------------------------------------
- #运行nginx所在的用户名和用户组
- user nobody nobody;
- #启动进程数
- worker_processes 2;
- worker_cpu_affinity 0010 0001 ;
- #worker_cpu_affinity 0001 0100 1000 0010 0001 0100 1000 0010;
- #全局错误日志及PID文件
- error_log /usr/local/nginx/logs/nginx_error.log crit;
- pid /usr/local/nginx/logs/nginx.pid;
- worker_rlimit_nofile 65535;
- #工作模式及连接数上限
- events
- {
- use epoll;
- worker_connections 65535;
- }
- #设定http服务器,利用它的反向代理功能提供负载均衡支持
- http{
- include mime.types;
- default_type application/octet-stream;
- server_names_hash_bucket_size 128;
- #设定请求缓冲
- client_header_buffer_size 32k;
- large_client_header_buffers 4 32k;
- client_max_body_size 8m;
- sendfile on;
- tcp_nopush on;
- keepalive_timeout 60;
- tcp_nodelay on;
- fastcgi_connect_timeout 300;
- fastcgi_send_timeout 300;
- fastcgi_read_timeout 300;
- fastcgi_buffer_size 64k;
- fastcgi_buffers 4 64k;
- fastcgi_busy_buffers_size 128k;
- fastcgi_temp_file_write_size 128k;
- #开启gzip模块
- gzip on;
- gzip_min_length 1k;
- gzip_buffers 4 16k;
- gzip_http_version 1.0;
- gzip_comp_level 2;
- gzip_types text/plain application/x-javascript text/css application/xml;
- gzip_vary on;
- #设定负载均衡列表
- upstream backend
- {
- #down 表示单前的server暂时不参与负载
- #weigth参数表示权值,权值越高被分配到的几率越大
- #server 192.168.3.69:80 weight=1;
- #max_fails :允许请求失败的次数默认为1.当超过最大次数时,返回proxy_next_upstream 模块定义的错误
- #fail_timeout:max_fails次失败后,暂停的时间。
- #backup: 其它所有的非backup机器down或者忙的时候,请求backup机器。所以这台机器压力会最轻。
- server 172.16.50.147:8081;
- server 172.16.50.147:8082;
- server 172.16.50.147:8083;
- server 172.16.50.147:8084;
- }
- #禁止通过ip访问站点
- #server{
- #server_name _;
- #return 404;
- #}
- #设定虚拟主机
- server {
- listen 80;
- server_name localhost;
- #对 / 所有做负载均衡 (本机nginx采用完全转发,所有请求都转发到后端的tomcat集群)
- location / {
- #设定网站的资源存放路径
- root /var/www ;
- #设定访问的默认首页地址
- index index.jsp index.htm index.html;
- #proxy_pass http://backend ;
- #保留用户真实信息
- include proxy.conf;
- proxy_set_header Host $host;
- proxy_set_header X-Real-IP $remote_addr;
- proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
- #proxy_cache cache;
- #proxy_store on;
- proxy_temp_path /root/cache;
- proxy_cache_valid 200 302 24h;#200和302状态码保存1小时
- proxy_cache_valid 301 1d;#301状态码保存一天
- proxy_cache_valid any 10h;#其它的保存一分钟
- if ( !-f \$request_filename) {
- proxy_pass http://backend;
- }
- }
- #状态监控部分
- location /nginx {
- stub_status on;
- access_log on;
- auth_basic "NginxStatus";
- auth_basic_user_file /usr/local/nginx/htpasswd;
- #允许访问的ip allow 127.0.0.1;
- }
- #定义访问日志的写入格式
- log_format access '$remote_addr - $remote_user [$time_local] "$request" '
- '$status $body_bytes_sent "$http_referer" '
- '"$http_user_agent" $http_x_forwarded_for';
- #设定访问日志的存放路径
- #access_log /usr/local/nginx/logs/access.log access;
- #设定access log
- access_log logs/access.log access;
- client_header_timeout 3m;
- client_body_timeout 3m;
- send_timeout 3m;
- sendfile on;
- tcp_nopush on;
- tcp_nodelay on;
- #keepalive_timeout 65; (这个参数如果启用,会出现未知错误,因此暂时取消)
- }
- }
- #---------------------------------
5.修改/usr/local/nginx/conf/nginx.conf配置文件后,请执行以下命令检查配置文件是否正确:
- #/usr/local/nginx/sbin/nginx -t
如果屏幕显示以下两行信息,说明配置文件正确:
- the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
- the configuration file /usr/local/nginx/conf/nginx.conf was tested successfully
如果提示unknown host,则可在服务器上执行:ping www.baidu.com如果也是同样提示unknown host则有两种可能: a、服务器没有设置DNS服务器地址,查看/etc/resolv.conf下是否设置,若无则加上 b、防火墙拦截 备注:conf/htpasswd 文件的内容用 apache 提供的 htpasswd 工具来产生即可,如下:
- htpasswd -c /usr/local/nginx/htpasswd admin
输入密码: 6、启动nginx的命令
- #/usr/local/nginx/sbin/nginx
这时,输入以下命令查看Nginx主进程号:
- netstat -ntlp
- ps -ef | grep nginx
查看 Nginx 运行状态 输入地址 http://172.16.50.147/nginx/,输入验证帐号密码,即可看到类似如下内容:
- Active connections: 328
- server accepts handled requests
- 9309 8982 28890
- Reading: 1 Writing: 3 Waiting: 324
7、停止nginx的命令
- #/usr/local/nginx/sbin/nginx -s stop
8,修改配置文件不停止服务,而重新加载新配置文件
- kill -HUP PID
- #/usr/local/nginx/sbin/nginx -s reload
8.纪念日把整站变成黑白色调 在nginx.conf配置文件的http {...}大括号内增加以下两行:
-
- #sub_filter '</head>' '<style type="text/css">html {filter:progid:DXImageTransform.Microsoft.BasicImage(grayscale=1); }</style></head>';
- #sub_filter_once on;