Nginx 是一个很强大的高性能Web和反向代理服务器,它具有很多非常优越的特性
下载地址:http://nginx.org/en/download.html
Windows: nginx-1.6.2.zip
Unix: nginx-1.6.2.tar.gz
tar zxvf nginx-1.6.2.tar.gz [-C {解压目录}]
cd nginx-1.6.2
./configure --prefix={安装目录} --with-http_stub_status_module --without-http_rewrite_module --without-http_gzip_module
make
make install
检查是否安装成功
进入安装目录
./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
./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.1 .安装Nginx时报错 ./configure: error: the HTTP rewrite module requires the PCRE library.
安装pcre-devel解决问题 yum -y install pcre-devel
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; }
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; }
在默认主机里面加上location或者你希望能访问到的主机里面。
server { listen *:80 default_server; server_name _; location /ngx_status { stub_status on; access_log off; #allow 127.0.0.1; #deny all; } }
# curl http://127.0.0.1/ngx_status |
Active connections: 11921 |
server accepts handled requests |
11989 11989 11991 |
Reading: 0 Writing: 7 Waiting: 42 |
这个可以配置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; }
如果不是在默认目录下,使用alias配置
location ^~ /project/ {
alias /home/qiyi/data/www/;
index index.html index.htm;
#charset GBK;
#expires 7d;
}