NGINX_初学文档

NGINX

Install

​ 安装nginx。

  1. 安装插件

    yum install gcc openssl openssl-devel pcre pcre-devel zlib zlib-devel -y
    
  2. 解压nginx 安装包

    root@localhost soft]# tar -zxvf nginx-1.16.0.tar.gz 
    
  3. 安装nginx

    prefix= 指定安装的位置

    [root@localhost nginx-1.16.0]# ./configure --prefix=/usr/local/nginx
    

    安装成功时,若是报错,则是环境配置出错。

    Configuration summary
      + using system PCRE library
      + OpenSSL library is not used
      + using system zlib library
    
      nginx path prefix: "/usr/local/nginx"
      nginx binary file: "/usr/local/nginx/sbin/nginx"
      nginx modules path: "/usr/local/nginx/modules"
      nginx configuration prefix: "/usr/local/nginx/conf"
      nginx configuration file: "/usr/local/nginx/conf/nginx.conf"
      nginx pid file: "/usr/local/nginx/logs/nginx.pid"
      nginx error log file: "/usr/local/nginx/logs/error.log"
      nginx http access log file: "/usr/local/nginx/logs/access.log"
      nginx http client request body temporary files: "client_body_temp"
      nginx http proxy temporary files: "proxy_temp"
      nginx http fastcgi temporary files: "fastcgi_temp"
      nginx http uwsgi temporary files: "uwsgi_temp"
      nginx http scgi temporary files: "scgi_temp"
    
    
  4. 编译成可执行程序

    通过gcc 编译

    [root@localhost nginx-1.16.0]# make
    
  5. 安装可执行程序

    [root@localhost nginx-1.16.0]# make install
    

    安装成功

    [root@localhost local]# cd nginx/
    [root@localhost nginx]# ll
    总用量 4
    drwxr-xr-x. 2 root root 4096 5月   3 14:12 conf
    drwxr-xr-x. 2 root root   40 5月   3 14:12 html
    drwxr-xr-x. 2 root root    6 5月   3 14:12 logs
    drwxr-xr-x. 2 root root   19 5月   3 14:12 sbin
    

Directory Structure

目录 说明
conf 配置文件中心
html 默认自带页面
logs 日志
sbin 启动程序

Starter

  1. 普通启动

    [root@localhost nginx]# cd sbin/
    [root@localhost sbin]# ./nginx
    
  2. 通过配置文件启动

    [root@localhost sbin]# /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
    
  3. 进程状态

    nginx 进程状态;

    master process 主线程,进行读取配置文件,并维护worker进程;

    worker process 进程,对请求进行实际处理;

    [root@localhost sbin]# ps -ef | grep nginx
    root     18751     1  0 14:26 ?        00:00:00 nginx: master process ./nginx
    nobody   18752 18751  0 14:26 ?        00:00:00 nginx: worker process
    root     28913 18260  0 14:29 pts/1    00:00:00 grep --color=auto nginx
    
  4. 关闭nginx

    1. 正常关闭

      ./nginx -s stop/reload

    [root@localhost sbin]# ./nginx -s stop
    
    1. 杀死进程;

      如果该进程有任务则当该进程处理完后就关闭

    kill -QUIT [主进程ID]

    [root@localhost sbin]# ps -ef | grep nginx
    root     28948     1  0 14:39 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
    nobody   28949 28948  0 14:39 ?        00:00:00 nginx: worker process
    root     28951 18260  0 14:39 pts/1    00:00:00 grep --color=auto nginx
    [root@localhost sbin]# kill -QUIT 28948
    
    1. 快速关闭;

      如果该进程有任务,立刻停止

    kill -TERM [主进程ID]

    [root@localhost sbin]# ps -ef | grep nginx
    root     28948     1  0 14:39 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
    nobody   28949 28948  0 14:39 ?        00:00:00 nginx: worker process
    root     28951 18260  0 14:39 pts/1    00:00:00 grep --color=auto nginx
    [root@localhost sbin]# kill -TERM 28948
    

Configuration check

配置检查,检查配置文件是否正确

[root@localhost sbin]# /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf -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

Version Information

查看nginx版本信息

./nginx -v

./nginx -V

[root@localhost sbin]# ./nginx -v
nginx version: nginx/1.16.0
[root@localhost sbin]# ./nginx -V
nginx version: nginx/1.16.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-36) (GCC) 
configure arguments: --prefix=/usr/local/nginx

Configuration File Interpretation

​ nginx 核心配置文件有三部分构成:基本配置,events配置,HTTP配置。

### ========start 基本配置

## 配置worker进程运行用户	[nobody|root]
#user  nobody;
## worker processes 进程数 通常等于CUP数量或者两倍CUP数量
worker_processes  1;
## 配置全局错误日志及类型 [debug|info|notice|warn|error|crit] 默认error crit-致命的错误
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;

###	========end 基本配置

### ==========start events配置
## 配置工作模式和链接数
events {
	#use epoll;
	## 配置每个worker进程链接数上线,nginx支持的总连接数等于worker_processes * worker_connections [MAX=55535]
    worker_connections  1024;
}
### ========end events配置

### =========start HTTP配置
http {
	## 支持的媒体类型 [无jsp]
    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  logs/access.log  main;
	
	## 开启高效文件传输模式
    sendfile        on;
    ## 防止网络阻塞,自动刷新
    #tcp_nopush     on;

	## 长连接超时时间
    #keepalive_timeout  0;
    keepalive_timeout  65;

	## 开启gzip的压缩
    #gzip  on;
	
	## HTTPS server 配置 
    server {
    	## 监听端口
        listen       80;
        ## 服务名称
        server_name  localhost;
        ## 字符集 [koi8-r 俄罗斯字符集]
        #charset koi8-r;
        ## 该server日志输出
        #access_log  logs/host.access.log  main;
        
        ## 匹配请求
        location / { ## 当访问路径中有[/]时,会被该location匹配到并进行处理,默认是/
            root   html; ## 配置服务器的默认网站跟目录位置,默认为nginx安装主要目录下的html目录
            index  index.html index.htm; ## 首页文件名称
        }
        ## 配置404页面
        #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;
        #}
        
		## 禁止访问 .htaccess文件 禁止外网访问
        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }
    
    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

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

	## HTTPS 配置https服务 ,安全的网络传输协议,加密传输,端口443
    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;
	## 证书 [wosign| 公司颁发公司...]
    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

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

}

Main Application

Static Web Site

​ 静态文件配置

​ URL = 本地IP地址 + location.server.listen + location [root]

location /cas {
    root   /usr/opt/cas-master;
    index  index.html index.htm;
}
静态符号 含义
~ 表示正则匹配,后面的类容是正则表达式
. 任意字符
\ 转义字符
|
$ 正则表达式结束符号
* 一个或者多个字符

Load Balancing

deploy

​ 负载均衡;无权重[weight] 则轮询访问,

upstream www.myweb.com {
    server 127.0.0.1:8080;
    server 127.0.0.1:8081;
}

​ 有权重[weight]则按照权重比例分配访问。

upstream www.myweb.com {
    server 127.0.0.1:8080 weight=3; ## weight 服务权重
    server 127.0.0.1:8081 weight=1;
}

​ 按照ip hash地址分配访问路径。能解决session 丢失问题

upstream www.myweb.com {
	ip_hash;
    server 127.0.0.1:8080;
    server 127.0.0.1:8081;
}

​ 最少连接[least_conn] 地址访问

upstream www.myweb.com {
	least_conn;
    server 127.0.0.1:8080;
    server 127.0.0.1:8081;
}

服务地址转发

sever{
	...
	location /myweb{
		## 转发地址
   		proxy_pass http://www.myweb.com;
	}
}

backups

​ 备份

​ backup - 其他所有的非backup 机器 down 的时候,才请求backup机器。

​ down - server 是down,不参数负载均衡,处于关闭,不转发。

upstream backserver{
    server 127.0.0.1:8080;
    server 127.0.0.1:8081 backup|down;
}

Static Proxy

​ 静态代理

location ~.*\.(js|css|htm|html|gif|jpg|jpeg|png|bmp|swf|ioc|rar|zip|txt|flv|mid|doc|ppt|pdf|xls|mp3|wma)${
    root: /opt/static;
}
location ~.*/(css|js|img|images){
     root: /opt/static;
}

Static And Dynamic Separation

​ 静态代理与负载均衡同时使用

Virtual Host

​ 多端口虚拟主机

server{
    listen: 8080;
    server_name: www.myweb.com;
    location /myweb {
        proxy_pass: http://www.myweb.com;
    }
}
server{
    listen: 8090;
    server_name: www.myweb.com;
    location /myweb {
        proxy_pass: http://www.p2p.com;
    }
}

​ 域名的虚拟主机

server{
    listen: 80;
    server_name: www.shop.com;
    location /myweb {
        proxy_pass: http://www.shop.com;
    }
}
server{
    listen: 80;
    server_name: www.p2p.com;
    location /myweb {
        proxy_pass: http://www.p2p.com;
    }
}
include /usr/local/nginx/vhost.conf

你可能感兴趣的:(nginx,nginx)