前端开发专用服务器nginx

在前端开发当中。既然前端能够成为独立的一套系统了。怎么少得了服务器与后端进行开发调试,对接接口调试呢。经过选择,还是选择轻量的nginx。下面说说大概使用吧。

目前最新的下载地址:传送门

 1.下载下来过后你只要关注里面的conf——nginx.conf的配置文件

 2.找到http块里面的server 修改配置 listen : 81 //监听端口。和 server_name  127.0.0.1; 本地服务器的ip

 3.打开浏览器输入127.0.0.1:81。有欢迎的界面就说明配置成功了。这样简单的一个本地服务器就能用了。。

 4.如果要做反向代理服务器的话,在server块找到 location /

         proxy_set_header        Host            $http_host;
         proxy_set_header        X-Real-IP       $remote_addr;
         proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
 
         location / {
                proxy_pass    http://192.168.1.2:80;
        }

/*********************这是一条进阶分割线。下面属于详细了解*********************************/

理解:

1.全局块:配置影响nginx全局的指令。一般有运行nginx服务器的用户组,nginx进程pid存放路径,日志存放路径,配置文件引入,允许生成worker process数等。

 

2、events块:配置影响nginx服务器或与用户的网络连接。有每个进程的最大连接数,选取哪种事件驱动模型处理连接请求,是否允许同时接受多个网路连接,开启多个网络连接序列化等。

 

3、http块:可以嵌套多个server,配置代理,缓存,日志定义等绝大多数功能和第三方模块的配置。如文件引入,mime-type定义,日志自定义,是否使用sendfile传输文件,连接超时时间,单连接请求数等。

 

4、server块:配置虚拟主机的相关参数,一个http中可以有多个server。

 

5、location块:配置请求的路由,以及各种页面的处理情况。

 

  1     #运行用户
  2     user www-data;   
  3     #启动进程,通常设置成和cpu的数量相等
  4     worker_processes  1;
  5 
  6     #全局错误日志及PID文件
  7     error_log  /var/log/nginx/error.log;
  8     pid        /var/run/nginx.pid;
  9 
 10     #工作模式及连接数上限
 11     events {
 12         use   epoll;             #epoll是多路复用IO(I/O Multiplexing)中的一种方式,但是仅用于linux2.6以上内核,可以大大提高nginx的性能
 13         worker_connections  1024;#单个后台worker process进程的最大并发链接数
 14         # multi_accept on;
 15     }
 16 
 17     #设定http服务器,利用它的反向代理功能提供负载均衡支持
 18     http {
 19          #设定mime类型,类型由mime.type文件定义
 20         include       /etc/nginx/mime.types;
 21         default_type  application/octet-stream;
 22         #设定日志格式
 23         access_log    /var/log/nginx/access.log;
 24 
 25         #sendfile 指令指定 nginx 是否调用 sendfile 函数(zero copy 方式)来输出文件,对于普通应用,
 26         #必须设为 on,如果用来进行下载等应用磁盘IO重负载应用,可设置为 off,以平衡磁盘与网络I/O处理速度,降低系统的uptime.
 27         sendfile        on;
 28         #tcp_nopush     on;
 29 
 30         #连接超时时间
 31         #keepalive_timeout  0;
 32         keepalive_timeout  65;
 33         tcp_nodelay        on;
 34        
 35         #开启gzip压缩
 36         gzip  on;
 37         gzip_disable "MSIE [1-6]\.(?!.*SV1)";
 38 
 39         #设定请求缓冲
 40         client_header_buffer_size    1k;
 41         large_client_header_buffers  4 4k;
 42 
 43         include /etc/nginx/conf.d/*.conf;
 44         include /etc/nginx/sites-enabled/*;
 45 
 46         #设定负载均衡的服务器列表
 47          upstream mysvr {
 48         #weigth参数表示权值,权值越高被分配到的几率越大
 49         #本机上的Squid开启3128端口
 50         server 192.168.8.1:3128 weight=5;
 51         server 192.168.8.2:80  weight=1;
 52         server 192.168.8.3:80  weight=6;
 53         }
 54          #设定实际的服务器列表 
upstream zp_server1{
server 127.0.0.1:8089;
}
56 server { 57 #侦听80端口 58 listen 80; 59 #定义使用www.xx.com访问 60 server_name www.xx.com;
          #首页
             index index.html
 61        #指向webapp的目录 
          root D:\01_Workspace\Project\github\zp\SpringNotes\spring-security\spring-shiro\src\main\webapp;
62 #设定本虚拟主机的访问日志 63 access_log logs/www.xx.com.access.log main;
         #代理配置参数
proxy_connect_timeout 180;

proxy_send_timeout 180;

proxy_read_timeout 180;

proxy_set_header Host $host;

proxy_set_header X-Forwarder-For $remote_addr;
#反向代理的路径(和upstream绑定),location 后面设置映射的路径
          location / {
proxy_pass http://zp_server1;
}
64 65 #默认请求 66 location / { 67 root /root; #定义服务器的默认网站根目录位置 68 index index.php index.html index.htm; #定义首页索引文件的名称 69 70 fastcgi_pass www.xx.com; 71 fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name; 72 include /etc/nginx/fastcgi_params; 73 } 74 75 # 定义错误提示页面 76 error_page 500 502 503 504 /50x.html; 77 location = /50x.html { 78 root /root; 79 } 80 81 #静态文件,nginx自己处理 82 location ~ ^/(images|javascript|js|css|flash|media|static)/ { 83 root /var/www/virtual/htdocs; 84 #过期30天,静态文件不怎么更新,过期可以设大一点,如果频繁更新,则可以设置得小一点。 85 expires 30d; 86 } 87 #PHP 脚本请求全部转发到 FastCGI处理. 使用FastCGI默认配置. 88 location ~ \.php$ { 89 root /root; 90 fastcgi_pass 127.0.0.1:9000; 91 fastcgi_index index.php; 92 fastcgi_param SCRIPT_FILENAME /home/www/www$fastcgi_script_name; 93 include fastcgi_params; 94 } 95 #设定查看Nginx状态的地址 96 location /NginxStatus { 97 stub_status on; 98 access_log on; 99 auth_basic "NginxStatus"; 100 auth_basic_user_file conf/htpasswd; 101 } 102 #禁止访问 .htxxx 文件 103 location ~ /\.ht { 104 deny all; 105 } 106 107 } 108 }

 

...              #全局块

events {         #events块
   ...
}

http      #http块
{
    ...   #http全局块
    server        #server块
    { 
        ...       #server全局块
        location [PATTERN]   #location块
        {
            ...
        }
        location [PATTERN] 
        {
            ...
        }
    }
    server
    {
      ...
    }
    ...     #http全局块
}

 

posted @ 2016-11-10 20:53  cygnet  阅读(399)  评论(0)    收藏  举报