Nginx安装说明
1.介绍
Nginx 是一个很强大的高性能Web和反向代理服务器,它具有很多非常优越的特性
2.安装
2.1 下载
下载地址:http://nginx.org/en/download.html
Windows: nginx-1.6.2.zip
Unix: nginx-1.6.2.tar.gz
2.2 解压
tar zxvf nginx-1.6.2.tar.gz [-C {解压目录}]
2.3 配置
cd nginx-1.6.2
./configure --prefix={安装目录} --with-http_stub_status_module --without-http_rewrite_module --without-http_gzip_module
make
make install
2.4 检测
检查是否安装成功
进入安装目录
./sbin/nginx -t
如果安装成功结果显示如下:
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
2.5 启动
./sbin/nginx
ie 浏览器中输入 http://{目标IP}
注意,这里nginx监听80端口,所以要在iptables里打开80端口。
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
接着访问这台机器的80的端口,如果请求成功,则说明配置成功。
为了操作方便,可以自己写一个nginx命令脚本,放到/etc/init.d下,执行方法如下:
启动:service nginx start
停止:service nginx stop
重启:service nginx reconfigure
查看状态:service nginx status
3.常见问题
3.1 .安装Nginx时报错 ./configure: error: the HTTP rewrite module requires the PCRE library.
安装pcre-devel解决问题 yum -y install pcre-devel
4.配置
4.1 基本配置方法
location / { proxy_pass http://127.0.0.1:8080; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } location ^~ /api/ { proxy_pass http://127.0.0.1:8081; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; }
4.2 负载均衡配置
upstream backend { #ip_hash; server 127.0.0.1:8081; server 10.10.136.85:8082; } upstream backendweb { ip_hash; server 127.0.0.1:8080; server 10.10.136.85:8081; } server { listen 80; server_name www.nfzo.com; #charset koi8-r; #access_log logs/host.access.log main; location / { #proxy_pass http://127.0.0.1:8080; proxy_pass http://backendweb; proxy_read_timeout 300; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } location ^~ /api/ { #proxy_pass http://127.0.0.1:8081; proxy_pass http://backend; proxy_read_timeout 300; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; }
4.3 启用nginx status配置
在默认主机里面加上location或者你希望能访问到的主机里面。
server { listen *:80 default_server; server_name _; location /ngx_status { stub_status on; access_log off; #allow 127.0.0.1; #deny all; } }
打开status页面
# curl http://127.0.0.1/ngx_status |
Active connections: 11921 |
server accepts handled requests |
11989 11989 11991 |
Reading: 0 Writing: 7 Waiting: 42 |
nginx status详解:
- active connections – 活跃的连接数量
- server accepts handled requests — 总共处理了11989个连接 , 成功创建11989次握手, 总共处理了11991个请求
- reading — 读取客户端的连接数.
- writing — 响应数据到客户端的数量
- waiting — 开启 keep-alive 的情况下,这个值等于 active – (reading+writing), 意思就是 Nginx 已经处理完正在等候下一次请求指令的驻留连接.
4.4 仅限域名访问配置
这个可以配置server 来限定,比如:
server { listen 80; server_name .*; location / { return 403; } } server { listen 80; server_name qimdev.qiyi.domain; #charset koi8-r; #access_log logs/host.access.log main; location / { proxy_pass http://127.0.0.1:8080; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; }
4.5 设置静态文件
如果不是在默认目录下,使用alias配置
location ^~ /project/ {
alias /home/qiyi/data/www/;
index index.html index.htm;
#charset GBK;
#expires 7d;
}