勿以浮沙筑高台

          • -

Nginx

Nginx
高性能web服务器

Nginx安装

1.下载Nginx

Nginx下载地址:http://nginx.org/en/download.html


wget http://nginx.org/download/nginx-1.18.0.tar.gz

2.安装Nginx需要的依赖环境
因为nginx是C语言编写的因此需要c语言环境

yum install -y gcc gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel

3.解压

 tar xf nginx-1.18.0.tar.gz

4.配置编译路径

cd nginx-1.18.0/
./configure --prefix=/usr/app/nginx

5.编译和安装

make & make install -j 4
cd /usr/app/nginx

5.启动

cd sbin
ps -ef | grep nginx

6.验证
访问linux服务器地址出现下图代表成功
在这里插入图片描述

Nginx配置文件

cd /usr/app/nginx/conf/
vim nginx.conf

Nginx.conf基本结构

// nginx全局块
...
​
// events块
events {
    ...
}
​
// http 块
http {
    // http全局块
    ...
    
    // server块
    server {
        ...
    }
    
    // http全局块
    ...
}
// upstream 块
upstream {
    
}

Nginx.conf配置文件解析

# 配置nginx的用户组 默认为nobody
#user  nobody;
#指定工作进程的个数
#默认是1个,具体可以根据服务器cpu数量进行设置,如果不知道cpu的数量,可以设置为auto
worker_processes  auto;

# 配置nginx的错误日志 格式为 log路径 log级别
# error_log 的日志级别为: debug info notice warn error crit alert emerg 紧急由低到高
# error_log的默认日志级别为error,那么就只有紧急程度大于等于error的才会记录在日志
# error_log 的作用域为 main http mail stream server location

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#Nginx运行文件地址
#pid        logs/nginx.pid;

#工作模式及连接数上限
events {
    worker_connections  1024;
}


http {
    # 文件扩展名和文件类型映射表 
    # mime.types 指text/css,image/jpeg,text/html这样的包含类型。
    # 在具mimetypes文件里能看见具体结构有哪些
    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"';

    // 访问日志配置路径
    // main
    # access_log  logs/access.log  main;

    # 是否开启0拷贝
    sendfile        on;
    
    # 是否 #减少网络报文段的数量
    #tcp_nopush     on;
    
    
    #keepalive_timeout  0;
    # 链接超时时间 默认 65s 
    keepalive_timeout  65;

    # 开始gzip压缩,降低带宽使用和加快传输速度,但增加了CPU的使用
    #gzip  on;

    server {
        # 端口号80
        listen       80;
        # 域名 Ip,一级域名,二级域名配置
        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;
        #}
    }


    # 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 server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    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;
    #    }
    #}
}

Nginx虚拟主机配置

更改linux文件的hosts

vim /etc/hosts
#格式 : IP  域名
192.168.31.5 www.fu.com
192.168.31.5 www.yan.com

因为用的虚拟机,更改本机hosts
Nginx配置和路由教程

在Nginx.conf中配置虚拟机

server {
    listen 80;
    server_name www.fu.com; # 域名区分
    
    location / {
        root html/fu;
        index index-1.html;
    }
}
server {
    listen 80;
    server_name www.yan.com;  # 域名区分
    
    location / {
        root html/fu;
        index index-2.html;
    }
}

在nginx.conf配置虚拟主机文件路径

include "vhosts/server.demo.conf";

创建虚拟主机文件路径

cd /usr/app/nginx/conf
mkdir -p vhosts
cp /usr/app/nginx/conf/nginx.conf /usr/app/nginx/conf/vhosts/server.demo.conf

然后将上面的server信息放进去。

创建server信息里的html文件

cd /usr/app/nginx/html/
echo "www.fu.com" >> index-1.html
echo "www.yan.com" >> index-2.html

重新加载配置文件并启动

 ./nginx -s reload
 ./nignx -t #启动

查看是否启动成功
进入log/error.log查看是否有signal process started这句话,有则代表成功

访问网站http://www.fu.com/http://www.yan.com/成功Nginx配置和路由教程

Nginx配置和路由教程

location匹配规则

= 开头表示精准匹配

# ~ 大小写敏感
# ~* 忽略大小写
# ^~ 只需匹配uri开头
# @ 定义一个命名的 location,在内部定向时使用,例如 error_page
location  [ = | ~ | ~* | ^~ ] /uri/ { ... }
location @name { ... }

任意匹配 location / {}
当找不到路由的时候就会走这个路由

server {
    listen 80;
    server_name www.fu.com; # 域名区分
    location / {
        root html/fu;
        index 404.html;
    }
}

只能匹配=号后面的内容。

// 精准匹配
 location = / {
        root html/fu;
        index 404.html;
    }
// ~* 忽略大小写
 location ~* / {
        root html/fu;
        index 404.html;
    }
// ~大小写敏感
 location ~ / {
        root html/fu;
        index 404.html;
    }

最长匹配原则

就是当找不到路由的时候,会按最长路径的路由进行匹配
比如:

 location /123 {
        root html/fu;
        index 404.html;
    }
   location /1234 {
        root html/fu;
        index 404.html;
    }
    #动静分离配置
 location ~* \.(gif|css|js|png|jpg|jpeg){
        root html/fu;
        index 404.html;
    }

我们访问1234598734时,没有这个路由则会匹配到1234这个路由,按匹配字符数量最多的进行路由。

标签: nginx, index, server, Nginx, location, html, 配置, 路由

相关文章推荐

添加新评论,含*的栏目为必填