nginx 测试开发

Nginx_结构/配置

2016-11-10  本文已影响69人  古佛青灯度流年

反向代理(--Nginx)

反向代理(Reverse Proxy)方式是指以代理服务器来接受internet上的连接请求,然后将请求转发给内部网络上的服务器,并将从服务器上得到的结果返回给internet上请求连接的客户端,此时代理服务器对外就表现为一个服务器。

一、准备

/usr/local/bin/nginx # 启动
# 或者
sudo nginx
/usr/local/bin/nginx -s reload #平滑重启
#或者
sudo nginx -s reload 
/usr/local/etc/nginx/nginx.conf #配置文件。

二、了解Nginx结构

main
events { 
       ....
}
http {
       .... 
       upstream myproject {
       ..... 
       }
       server {
         ....
             location { 
              .... 
            }
       } 
       server {
         ....
             location {
                 .... 
             } 
      } 
     ....
}

下面是一个main区域,它是一个全局的设置:

user nobody nobody;
worker_processes 2;
error_log /usr/local/var/log/nginx/error.log notice;
pid /usr/local/var/run/nginx/nginx.pid;
worker_rlimit_nofile 1024;

events模块来用指定nginx的工作模式和工作模式及连接数上限,一般是这样:

events { 
        use kqueue; #mac平台 
        worker_connections 1024;
}

-use用来指定Nginx的工作模式。Nginx支持的工作模式有select、poll、kqueue、epoll、rtsig和/dev/poll。其中select和poll都是标准的工作模式,kqueue和epoll是高效的工作模式,不同的是epoll用在Linux平台上,而kqueue用在BSD系统中,因为Mac基于BSD,所以Mac也得用这个模式,对于Linux系统,epoll工作模式是首选。

http模块可以说是最核心的模块了,它负责HTTP服务器相关属性的配置,它里面的server和upstream子模块,至关重要,等到反向代理和负载均衡以及虚拟目录等会仔细说。

http{
         include mime.types;
         default_type application/octet-stream; 
          log_format main '$remote_addr - $remote_user [$time_local] "$request" ' 
                                          '$status $body_bytes_sent "$http_referer" '
                                          '"$http_user_agent"   "$http_x_forwarded_for"';
         access_log /usr/local/var/log/nginx/access.log main;
         sendfile on; 
         tcp_nopush on;
         tcp_nodelay on;
         keepalive_timeout 10; 
         #gzip on; 
         upstream myproject {
                 ..... 
         }   
          server { 
                    ....
           }
}
127.0.0.1 - - [21/Apr/2015:18:09:54 +0800] "GET /index.php HTTP/1.1" 200 87151 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.76 Safari/537.36"

server模块是http的子模块,它用来定一个虚拟主机

server {
         listen 8080;
         server_name localhost 192.168.12.10 www.yangyi.com; # 全局定义,如果都是这一个目录,这样定义最简单。
         root /Users/yangyi/www;
         index index.php index.html index.htm; 
        charset utf-8;
         access_log usr/local/var/log/host.access.log main;
         aerror_log usr/local/var/log/host.error.log error; 
          ....
}

location模块是nginx中用的最多的,也是最重要的模块了,什么负载均衡啊、反向代理啊、虚拟域名啊都与它相关
location根据它字面意思就知道是来定位的,定位URL,解析URL,所以,它也提供了强大的正则匹配功能,也支持条件判断匹配,用户可以通过location指令实现Nginx对动、静态网页进行过滤处理。像我们的php环境搭建就是用到了它。
1.设定默认首页和虚拟机目录

location / {
     root /Users/yangyi/www; 
     index index.php index.html index.htm;
}

2.location 还有一种方式就是正则匹配,开启正则匹配这样:location ~。后面加个~
下面这个例子是运用正则匹配来链接php。我们之前搭建环境也是这样做:

location ~ \.php$ {
       root /Users/yangyi/www; 
       fastcgi_pass 127.0.0.1:9000;
       fastcgi_index index.php; 
       include fastcgi.conf;
}

upstream 模块负责负载均衡模块,通过一个简单的调度算法来实现客户端IP到后端服务器的负载均衡。先学习怎么用,具体的使用实例以后再说。

upstream iyangyi.com{
         ip_hash; 
         server 192.168.12.1:80;
         server 192.168.12.2:80 down; 
         server 192.168.12.3:8080 max_fails=3 fail_timeout=20s; 
         server 192.168.12.4:8080;
}

在上面的例子中,通过upstream指令指定了一个负载均衡器的名称iyangyi.com。这个名称可以任意指定,在后面需要的地方直接调用即可。里面是ip_hash这是其中的一种负载均衡调度算法,下面会着重介绍。紧接着就是各种服务器了。用server关键字表识,后面接ip。

三、 基于域名的虚拟主机(实践)

假设我们在本地开发有2个项目,想要通过Nginx反向代理:

1.在Host文件中配置本地host:/private/etc/hosts
127.0.0.1 www.aaa.com
127.0.0.1 api.bbb.com
2.设置server

首先找到项目根目录:

/User/www/
/User/api/

然后确保根目录中有index.php/index.html文件;
第三步,修改nginx.conf,我们要把server放在指定的目录app,(即:/usr/local/etc/nginx/app/)需要修改为:

main
events {
           ....
}
http {
           ....
           include app/*.conf; 
          #或者          
          #include app/api.bbb.conf; 
          #include app/www.aaa.conf; 
}

下面根据上面准备的可以配置server了:

# www.aaa.conf
server { 
            listen 80;
            server_name www.aaa.com ; 
            root  /User/www/; 
            index index.php index.html index.htm; 
            access_log /usr/local/var/log/nginx/aaa.log main; 
            error_log /usr/local/var/log/nginx/aaa.log error; 
            location ~ \.php$ { 
                      fastcgi_pass 127.0.0.1:9000;
                     fastcgi_index index.php; 
                     include fastcgi.conf; 
            }
}
# api.bbb.conf
server { 
            listen 80;
            server_name api.bbb.com ; 
            root  /User/api/; 
            index index.php index.html index.htm; 
            access_log /usr/local/var/log/nginx/bbb.log main; 
            error_log /usr/local/var/log/nginx/bbb.log error; 
            location ~ \.php$ { 
                      fastcgi_pass 127.0.0.1:9000;
                     fastcgi_index index.php; 
                     include fastcgi.conf; 
            }
}

这样2个很精简的虚拟域名就搭建好了。重启下nginx,然后打开浏览器访问一下这2个域名,就能看到对应的域名内容了。

@这些暂时是我用到的,以后如果有在深层次的需求,再向深部扩展

@ 雾霾-2016-11-10 19:02:50
参考资料:@

上一篇下一篇

猜你喜欢

热点阅读