nginx、ansible和tomcat

Nginx基本概述 day38

2019-09-26  本文已影响0人  静如止水yw

介绍Nginx
Nginx的使用场景
Nginx的安装 配置 启动
Nginx搭建游戏网站
Nginx配置虚拟机的三种方式


一、介绍Nginx

1.什么是Nginx

Nginx是一个高性能的和反向代理web服务器
特点:
① 开源,体现在直接获取Nginx的源代码
② 高性能,体现在支持海量的并发
③ 高可靠,体现在服务稳定

2.选择Nginx的原因

二、Nginx的使用场景

三、Nginx安装 配置 启动

[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

b.使用yum直接安装

 [root@web01 ~]# yum install nginx -y
 [root@web01 ~]# systemctl start nginx
[root@web~]# 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 epool;                                      # 使用epool网络模型
}
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;                           # 启用压缩功能
    include /etc/nginx/conf.d/*.conf;    # 包含哪些文件
}


示例:使用server配置网站,每个server{}代表一个网站
server {
            listen 80;                          # 监听的端口
            server_name test.wang.com;          # 域名

            location / {                        # 控制网站访问的路径
                            root /code ...;
                            index index.html index.txt...;
            }
}

PS:Nginx中的http、server、location三者之间的关系
① http标签主要用来解决用户的请求与响应
② server标签主要用来响应具体的某一个网站
③ location标签主要用来匹配网站具体url路径
④ http{}层下允许有多个server{},可以有多个网站,一个serve{}下又允许有多个location{},每个网站的uri路径不同,所以要分别进行匹配。

四、Nginx搭建游戏网站

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

② 编写Nginx配置文件

[root@web/etc/nginx/conf.d]# vim game.wang.com.conf
server {
           listen 80;                   # 该网站提供访问的端口
           server_name game.wang.com;   # 访问该网站的域名

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

③ 根据配置进行初始化环境创建

[root@web/etc/nginx/conf.d]# mkdir /code

④ 上传代码

[root@web/etc/nginx/conf.d]# cd /code
[root@web/code]# rz html5.zip
[root@web/code]# unzip html5.zip 

⑤ 检测语法

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

⑥ 重载服务

[root@web/code]# systemctl restart nginx

⑦ 配置域名解析(hosts劫持)
10.0.0.7 game.wang.com


域名解析

搭建多个游戏网站同理,与搭建一个是一样。


多个网站

五、Nginx配置虚拟机的三种方式

[root@web/etc/nginx/conf.d]# vim ip_eth0.conf
server {
            listen 10.0.0.7:80;
            
            lication / {
            root /ip1;
            index index.html;
             }
}

[root@web/etc/nginx/conf.d]# vim ip_eth0.conf
server {
            listen 172.16.1.7:80;
            
            lication / {
            root /ip2;
            index index.html;
             }
}

[root@web/etc/nginx/conf.d]# mkdir /ip1 /ip2 -p
[root@web/etc/nginx/conf.d]# echo "10..." > /ip1/index.html
[root@web/etc/nginx/conf.d]# echo "172..." > /ip2/index.html
[root@web/etc/nginx/conf.d]# systemctl restart nginx

测试访问:


测试访问

② 基于端口的配置方式

[root@web/etc/nginx/conf.d]# vim port.conf
server {
            listen 81;
            
            lication / {
            root /81;
            index index.html;
             }
}

server {
            listen 82;
            
            lication / {
            root /82;
            index index.html;
             }
}

server {
            listen 83;
            
            lication / {
            root /83;
            index index.html;
             }
}

[root@web/etc/nginx/conf.d]# mkdir /81 /82 /83 -p
[root@web/etc/nginx/conf.d]# echo "81" > /81/index.html
[root@web/etc/nginx/conf.d]# echo "82" > /82/index.html
[root@web/etc/nginx/conf.d]# echo "83" > /83/index.html
[root@web/etc/nginx/conf.d]# systemctl restart nginx

测试访问


测试访问
上一篇 下一篇

猜你喜欢

热点阅读