Nginx basic overview

io network model introduced

1. Introduction Nginx

  Nginx是一个高性能的HTTP和反向代理web服务器

2. The common Web server

  httpd
  Nginx
  Tengine
  OpenResty

3. Presentation of Nginx scenarios

1.代理
2.负载均衡
3.代理缓存   (proxy_cache)
4.静态资源
5.动静分离
6.Https
冰山模型中的一角  ---> 还有很多个使用场景

4.Nginx start installation configuration

第一种: 源码安装
第二种: yum  --> 官方仓库  新   配置容易入手------》推荐使用
第三种: yum  --> epel仓库    旧   配置比较复杂

1.安装官方仓库源
[root@web01 ~]# cat /etc/yum.repos.d/nginx.repo 
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key

2.使用yum直接安装
[root@web01 ~]# yum install nginx -y

3.启动nginx
[root@web01 ~]# systemctl restart nginx

5.Nginx Configuration understand

[root@web01 ~]# cat /etc/nginx/nginx.conf

user  nginx;                                    # nginx进程的用户身份
worker_processes  1;                            # nginx的工作进程数量
error_log  /var/log/nginx/error.log warn;       # 错误日志的路径 [警告级别才会记录]
pid        /var/run/nginx.pid;                  # 进程运行后,会产生一个pid


events {                                        # 事件模型
    worker_connections  1024;                   # 每个work能够支持的连接数
    use epoll;                                  # 使用epoll网络模型
}


http {                                          # 接收用户的http请求
    include       /etc/nginx/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  /var/log/nginx/access.log  main;    # 访问日志的路径
    #sendfile        on;
    #tcp_nopush     on;
    keepalive_timeout  65;      #长链接超时时间
    #gzip  on;                  #启用压缩功能
    
    
#使用Server配置网站, 每个Server{}代表一个网站
    server {
        listen 80;
        server_name test.oldxu.com;
        
        location / {                    #控制网站访问的路径
            root ...;
        }
    }

    include /etc/nginx/conf.d/*.conf;       包含哪些文件
}


PS: Nginx中的http、server、location之间的关系是?
http       标签主要用来解决用户的请求与响应。
server     标签主要用来响应具体的某一个网站。
location   标签主要用于匹配网站具体url路径。

http{} 层下允许有多个Server{},可以有多个网站.
一个Server{} 下又允许有多个location{}    每个网站的uri路径不同,所以要分别进行匹配.

6.Nginx build a gaming site

1. Comment out the default Web site before

[root@web01 html]# cd /etc/nginx/conf.d/
[root@web01 conf.d]# gzip default.conf 

2. Write gaming sites Nginx configuration file

[root@web01 conf.d]# cat game.oldxu.com.conf 
server {
    listen 80;                  #该网站提供访问的端口
    server_name game.oldxu.com; #访问该网站的域名
    
    location / {
        root /code;
        index index.html;
    }
}

The Nginx configuration file to initialize the environment

[root@web01 conf.d]# mkdir /code

4. Upload the game source code file

[root@web01 conf.d]# cd /code/
[root@web01 code]# rz html5.zip
[root@web01 code]# unzip html5.zip 

5. Detection Syntax

[root@web01 code]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

6. Overload Services

[root@web01 code]# systemctl restart nginx

7. Configure DNS hosts

PC机:C:\Windows\System32\drivers\etc

The overall process 8.Nginx access

http://    game.oldxu.com     /      game/yibihua/index.html

请求的uri:   /game/yibihua/index.html
真实映射位置:   /code/game/yibihua/index.html

9.Nginx build more gaming website ---> Web Hosting

Web Hosting: run multiple sets of sites on a single server

Nginx virtual host configuration has the following three ways:
方式一、基于主机多IP方式                   10.0.0.7  172.16.1.7
方式二、基于端口的配置方式                   80 81 82 83
方式三、基于名称方式(多域名方式)           test1 test2 test3       <---推荐
  • A way, IP-based multi-host mode
[root@web01 conf.d]# cat ip_eth0.conf 
server {
    listen 10.0.0.7:80;
    location / {
        root /ip1;
        index index.html;
    }
}
server {
    listen 172.16.1.7:80;
    location / {
        root /ip2;
        index index.html;
    }
}
[root@web01 conf.d]# mkdir /ip1 /ip2
[root@web01 conf.d]# echo "10...." > /ip1/index.html
[root@web01 conf.d]# echo "172...." > /ip2/index.html
[root@web01 conf.d]# systemctl restart nginx


测试访问
[root@web01 ~]# curl http://10.0.0.7
10.... 
[root@web01 ~]# curl http://172.16.1.7
172....
  • Second way, based on 818 283 configuration port
    within the company have several systems in hopes deployed on a single server, and network and no domain name.
    So, we can through the same IP, different ports, different access web pages .
[root@web01 conf.d]# cat port.conf 
server {
    listen 81;

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

server {
    listen 82;

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

server {
    listen 83;

    location / {
        root /83;
        index index.html;
    }
}
[root@web01 conf.d]# mkdir /81 /82 /83
[root@web01 conf.d]# echo "81" > /81/index.html
[root@web01 conf.d]# echo "82" > /82/index.html
[root@web01 conf.d]# echo "83" > /83/index.html
  • Three ways, three sites running on the same server, you only need to achieve access through a different domain:
1. game.cyw.com
[root@web01 code]# cat /etc/nginx/conf.d/game.cyw.com.conf 
server {
    listen 80;
    server_name game.cyw.com;

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

}

2.  game2.yinwu.com
[root@web01 code2]# cat /etc/nginx/conf.d/game2.yinwu.com.conf 
server {
    listen 80;
    server_name game2.yinwu.com;

location / {
    root /code2/TangMen;
    index index.html;

    }

}

3.  game3.yinwu.com
[root@web01 code3]# cat /etc/nginx/conf.d/game3.yinwu.com.conf 
server {
    listen 80;
    server_name game3.yinwu.com;
location / {
    root /code3/Mota;
    index index.html;
    }
}

Guess you like

Origin www.cnblogs.com/yinwu/p/11529587.html