Nginx认识及配置

Nginx的初步认识及配置

什么是Nginx

是一个高性能的反向代理服务器
正向代理代理的是客户端
反向代理代理的是服务端

Apache、Tomcat、Nginx

静态web服务器
jsp/servlet服务器 tomcat

Nginx安装

  1. 下载tar包
    http://nginx.org/download/nginx-1.14.2.tar.gz
    
  2. 解压
    tar -zxvf nginx-1.14.2.tar.gz 
    
  3. ./configure [–prefix]
    yum install pcre-devel
    yum install zlib-devel
    ./configure --prefix=/home/nginx
    
  4. 安装
    make && make install
    

启动和停止

1. sbin/nginx
2. ./nginx -s stop

nginx.conf

event

events {
#仅对指定的客户端输出debug级别的日志: 语法:debug_connection[IP|CIDR]
#这个设置项实际上属于事件类配置,因此必须放在events{……}中才会生效。它的值可以是IP地址或者是CIRD地址。
	#debug_connection 10.224.66.14;  #或是debug_connection 10.224.57.0/24
#这样,仅仅以上IP地址的请求才会输出debug级别的日志,其他请求仍然沿用error_log中配置的日志级别。
#注意:在使用debug_connection前,需确保在执行configure时已经加入了--with-debug参数,否则不会生效。
   worker_connections  1024;
}

http

虚拟主机配置

server {
       listen       80;
       server_name  localhost;

       #charset koi8-r;

       #access_log  logs/host.access.log  main;

       location / {
           root   html;
           index  index.html index.htm;
       }

       #error_page  404              /404.html;

       # redirect server error pages to the static page /50x.html
       #
       error_page   500 502 503 504  /50x.html;
       location = /50x.html {
           root   html;
       }

       # proxy the PHP scripts to Apache listening on 127.0.0.1:80
       #
       #location ~ \.php$ {
       #    proxy_pass   http://127.0.0.1;
       #}

       # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
       #
       #location ~ \.php$ {
       #    root           html;
       #    fastcgi_pass   127.0.0.1:9000;
       #    fastcgi_index  index.php;
       #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
       #    include        fastcgi_params;
       #}

       # deny access to .htaccess files, if Apache's document root
       # concurs with nginx's one
       #
       #location ~ /\.ht {
       #    deny  all;
       #}
   }

基于IP的虚拟主机

基于端口号的虚拟主机

server {
       listen       8000;
       #listen       localhost:8080;
       server_name  localhost;

       location / {
           root   html;
           index  index.html index.htm;
       }
   }

基于域名的虚拟机

server {
   listen    80;
   server_name  www.abc.com;
   location / {
     root html;
     index index.html;
   }
 }
 server {
   listen    80;
   server_name bbs.abc.com;
   location / {
    root  html;
    index bbs.html;
   }
 }
 server {
   listen    80;
   server_name ask.abc.com;
   location / {
     root html;
     index ask.html;
   }
 }

location

配置语法

location [= | ~* | ^~ ] /uri/ {…}

配置规则

location = /uri 精准匹配
location ^~ /uri 前缀匹配
location ~ /uri
location / 通用匹配

规则的优先级

1 location = /
2 location = /index
3 location ^~ /article/
4 location ^~ /article/files/
5 location ~ \.(gif|png|js|css)$
6 location /
http://192.168.11.154/ 
http://192.168.11.154/index ->2
http://192.168.11.154/article/files/1.txt ->4
http://192.168.11.154/mic.png ->5
  1. 精准匹配是优先级最高
  2. 普通匹配(最长的匹配)
  3. 正则匹配

实际使用建议

location =/ {
 
}
location / {
 
}
location ~* \.(gif|....)${
 
}

Nginx模块

内置模块:反向代理、email、nginx core。。。

模块分类

  1. 核心模块 ngx_http_core_module
  2. 标准模块 http模块
  3. 第三方模块

如何添加第三方模块

  1. 原来所安装的配置,你必在重新安装新模块的时候,加上
  2. 不能直接make install

安装方法

./configure --prefix=/安装目录 --add-module = /第三方模块的目录

./configure --prefix=/home/cj/nginx --with-http_stub_status_module --with-
http_random_index_module

cp objs/nginx $nginx_home/sbin/nginx
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值