nginx 安装

   

一、安装Nginx:

1 : wget下载: http://nginx.org/download/nginx-1.4.2.tar.gz

2 : 进行安装: tar -zxvf nginx-1.6.2.tar.gz

3 : 下载锁需要的依赖库文件:

yum install pcre

yum install pcre-devel

yum install zlib

yum install zlib-devel

timg (4).jpg

4 : 进行configure配置:cd nginx-1.6.2 && ./configure --prefix=/usr/local/nginx 查看是否报错

5 : 编译安装 make && make install

6 : 启动Nginx:

cd /usr/local/nginx目录下: 看到如下4个目录

....conf 配置文件

... html 网页文件

...logs 日志文件

...sbin 主要二进制程序

 

启动命令:/usr/local/nginx/sbin/nginx -s start 关闭(stop)重启(reload)

 

成功:查看是否启动(netstat -ano | grep 80)

失败:可能为80端口被占用等。

最终:

浏览器访问地址:http://localhost:80 (看到欢迎页面即可)

 

二、使用Nginx:简单与单台Tomcat整合

a) 首先找到nginx.conf文件:vim /usr/local/nginx/conf/nginx.conf

server {

listen 80;

server_name localhost:80;

location / {

proxy_pass http://localhost:8080;

}

//...others

}

 

三、详细使用(nginx就是去配置其文件而已),如下所示:

#开启进程数<=CPU数

worker_processes1;

 

#错误日志保存位置

#error_loglogs/error.log;

#error_loglogs/error.lognotice;

#error_loglogs/error.loginfo;

 

#进程号保存文件

#pidlogs/nginx.pid;

 

#等待事件

events{

#每个进程最大连接数(最大连接=连接数x进程数)

#每个worker允许同时产生多少个链接,默认1024

worker_connections1024;

}

http{

#文件扩展名与文件类型映射表

includemime.types;

#默认文件类型

default_typeapplication/octet-stream;

#日志文件输出格式这个位置相于全局设置

#log_formatmain\'$remote_addr-$remote_user[$time_local]"$request"\'

#\'$status$body_bytes_sent"$http_referer"\'

#\'"$http_user_agent""$http_x_forwarded_for"\';

#请求日志保存位置

#access_loglogs/access.logmain;

#打开发送文件

sendfileon;

#tcp_nopushon;

#连接超时时间

#keepalive_timeout0;

keepalive_timeout65;

#打开gzip压缩

#gzipon;

#设定请求缓冲

client_header_buffer_size1k;

large_client_header_buffers44k;

#设定负载均衡的服务器列表

upstreammyproject{

#weigth参数表示权值,权值越高被分配到的几率越大

#max_fails当有#max_fails个请求失败,就表示后端的服务器不可用,默认为1,将其设置为0可以关闭检查

#fail_timeout在以后的#fail_timeout时间内nginx不会再把请求发往已检查出标记为不可用的服务器

#这里指定多个源服务器,ip:端口,80端口的话可写可不写

server192.168.1.78:8080weight=5max_fails=2fail_timeout=600s;

#server192.168.1.222:8080weight=3max_fails=2fail_timeout=600s;

}

 

#第一个虚拟主机

server{

#监听IP端口

listen80;

#主机名

server_namelocalhost;

#设置字符集

#charsetkoi8-r;

#本虚拟server的访问日志相当于局部变量

#access_loglogs/host.access.logmain;

#对本server"/"启用负载均衡

location/{

#root/root;#定义服务器的默认网站根目录位置

#indexindex.phpindex.htmlindex.htm;#定义首页索引文件的名称

proxy_passhttp://myproject;#请求转向myproject定义的服务器列表

#以下是一些反向代理的配置可删除.

#proxy_redirectoff;

#proxy_set_headerHost$host;

#proxy_set_headerX-Real-IP$remote_addr;

#proxy_set_headerX-Forwarded-For$proxy_add_x_forwarded_for;

#client_max_body_size10m;#允许客户端请求的最大单文件字节数

#client_body_buffer_size128k;#缓冲区代理缓冲用户端请求的最大字节数,

#proxy_connect_timeout90;#nginx跟后端服务器连接超时时间(代理连接超时)

#proxy_send_timeout90;#后端服务器数据回传时间(代理发送超时)

#proxy_read_timeout90;#连接成功后,后端服务器响应时间(代理接收超时)

#proxy_buffer_size4k;#设置代理服务器(nginx)保存用户头信息的缓冲区大小

#proxy_buffers432k;#proxy_buffers缓冲区,网页平均在32k以下的话,这样设置

#proxy_busy_buffers_size64k;#高负荷下缓冲大小(proxy_buffers*2)

#proxy_temp_file_write_size64k;#设定缓存文件夹大小,大于这个值,将从upstream服务器传

}

location/upload{

aliase:/upload;

}

#设定查看Nginx状态的地址

location/NginxStatus{

stub_statuson;

access_logoff;

#allow192.168.0.3;

#denyall;

#auth_basic"NginxStatus";

#auth_basic_user_fileconf/htpasswd;

}

#error_page404/404.html;

#redirectservererrorpagestothestaticpage/50x.html

#定义错误提示页面

error_page500502503504/50x.html;

location=/50x.html{

roothtml;

}

#proxythePHPscriptstoApachelisteningon127.0.0.1:80

#

#location~.php${

#proxy_passhttp://127.0.0.1;

#}

#passthePHPscriptstoFastCGIserverlisteningon127.0.0.1:9000

#

#location~.php${

#roothtml;

#fastcgi_pass127.0.0.1:9000;

#fastcgi_indexindex.php;

#fastcgi_paramSCRIPT_FILENAME/scripts$fastcgi_script_name;

#includefastcgi_params;

#}

#denyaccessto.htaccessfiles,ifApache\'sdocumentroot

#concurswithnginx\'sone

 

location~/.ht{

denyall;

}

}

anothervirtualhostusingmixofIP-,name-,andport-basedconfiguration

 

server{

#多监听

listen8000;

#主机名

listensomename:8080;

server_namesomenamealiasanother.alias;

 

location/{

#WEB文件路径

roothtml;

#默认首页

indexindex.htmlindex.htm;

}

}

#HTTPSserverHTTPSSSL加密服务器

server{

listen443;

server_namelocalhost;

 

sslon;

ssl_certificatecert.pem;

ssl_certificate_keycert.key;

ssl_session_timeout5m;

ssl_protocolsSSLv2SSLv3TLSv1;

ssl_ciphersHIGH:!aNULL:!MD5;

ssl_prefer_server_cipherson;

location/{

roothtml;

indexindex.htmlindex.htm;

}

}

}