Nginx安装说明

2015/07/10 14:34
阅读数 62

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.配置

     基本配置方法

?
1
2
3
4
5
6
7
8
9
10
11
12
13
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;
        }


5.负载均衡配置

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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://<span></span>backendweb<span></span>;
               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;
        }

展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部