V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
• 请不要在回答技术问题时复制粘贴 AI 生成的内容
hansonwang99
V2EX  ›  程序员

Nginx 服务器开箱体验

  •  
  •   hansonwang99 ·
    hansonwang99 · May 26, 2018 · 4499 views
    This topic created in 2904 days ago, the information mentioned may be changed or developed.

    profile


    概述

    Nginx 是高性能的轻量级 WEB 服务器,由于其提供 HTTP 代理和反向代理、负载均衡、缓存等一系列重要特性,从而广泛应用于当今的 WEB 服务之中,学习其很有必要。笔者也是从一个初学者开始学习并记录,希望后续渐渐深入。

    对了,为什么不用 Docker 来启动 Nginx 呢?因为不想啊!


    实验环境

    • CentOS 7.4 64Bit
    • Nginx 1.14.0 稳定版

    下载 Nginx

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

    安装 Nginx

    • 安装第三方库
    yum -y install make zlib zlib-devel gcc-c++ libtool openssl openssl-devel
    yum -y install pcre-devel
    

    安装成功以后可以查看 pcre 版本: pcre-config — version

    查看 pcre 版本

    • 解压安装包
    tar zxvf nginx-1.14.0.tar.gz
    
    • 从 configure 脚本自动生成 Makefile
    cd /root/nginx-1.14.0
    ./configure --prefix=/usr/local/webserver/nginx
    

    这里的 — prefix 选项是指定 Nginx 的安装路径,这里我是指定安装到路径:/usr/local/webserver/nginx

    • 编译安装
    cd /root/nginx-1.14.0
    make && make install
    

    编译安装过程如下,直到完成

    编译安装过程如下

    • 查看 Nginx 版本
    /usr/local/webserver/nginx/sbin/nginx -v
    

    查看 Nginx 版本

    • 查看安装后的目录

    总共四个目录:confhtmllogssbin

    查看安装后的目录


    启动 Nginx

    /usr/local/webserver/nginx/sbin/nginx
    

    浏览器访问 Nginx 成功(注意:由于我的机子上 80 端口被占用了,因此我将 Nginx 起在 81 端口上了):

    浏览器访问成功


    常用指令

    • 测试 nginx 测试文件是否正确
    /usr/local/webserver/nginx/sbin/nginx -t
    
    • 指定配置文件启动
    /usr/local/webserver/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
    
    • 重启 nginx
    /usr/local/webserver/nginx/sbin/nginx -s reload            # 重新载入配置文件
    /usr/local/webserver/nginx/sbin/nginx -s reopen          #重启 Nginx
    
    • 停止 nginx
    /usr/local/webserver/nginx/sbin/nginx -s stop              # 停止 Nginx
    

    Nginx 服务器配置实例与实验

    准备配置文件 nginx.conf 如下:

    // 全局配置
    user  nobody  nobody;
    worker_processes  3;
    error_log  logs/error.log;
    pid  logs/nginx.pid;
    
    // events 块配置
    events {
        use epoll;
        worker_connections  1024;
    }
    
    // http 块配置
    http {
        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;
        keepalive_timeout  65;
    
        // 虚拟主机 1:基于域名 codesheep.com
        server {
            listen       8088;
            server_name  codesheep;
    
            access_log  /codesheep/webserver/server1/log/access.log;
            error_page  404  /404.html;
    
            location /server1/location1 {
                root   /codesheep/webserver;
                index  index.server1-location1.htm;
            }
    
            location /server1/location2 {
                root   /codesheep/webserver;
                index  index.server1-location2.htm;
            }
    
        }
    
        // 虚拟主机 2:基于 IP 地址:192.168.31.177
        server {
            listen       8089;
            server_name  192.168.31.177;
    
            access_log  /codesheep/webserver/server2/log/access.log;
            error_page  404  /404.html;
    
            location /server2/location1 {
                root   /codesheep/webserver;
                index  index.server2-location1.htm;
            }
    
            location /srv2/loc2 {
                alias   /codesheep/webserver/server2/location2/;
                index  index.server2-location2.htm;
            }
    
            location = /404.html {
                root /codesheep/webserver/;
                index 404.html;
            }
        }
    }
    

    很明显,在上述配置文件中配置了两个虚拟主机:一个 基于域名一个基于 IP 地址

    为了验证该配置的正确性,我们对照此配置,构建一个与其对应的静态站点,其目录结构如下:

    站点结构

    现在可以启动 Nginx 服务器,并在浏览器中进行测试

    • 测试站点 server1 的访问

    server1/location1

    server1/location2

    这就说明配置文件中虚拟主机 1 配置生效!

    注意:这里域名 www.codesheep.com 之所以能被解析识别,是因为本地配置了 DNS 服务器!

    • 测试站点 server2 的访问

    server2/location1

    srv2/loc2

    这就说明配置文件中虚拟主机 2 配置生效!


    后记

    作者更多的原创文章:在 V2EX 作者主页

    如果有兴趣,可以看看作者一些关于容器化、微服务化方面的文章:


    19 replies    2018-05-27 17:47:27 +08:00
    glues
        1
    glues  
       May 26, 2018 via iPhone
    大..大清又忘了?
    dobelee
        2
    dobelee  
       May 26, 2018 via Android   ❤️ 1
    8102 了?😳
    larryli1995
        3
    larryli1995  
       May 26, 2018 via iPhone
    谢谢分享
    miao
        4
    miao  
       May 26, 2018 via Android
    新手还是有很多的。

    这篇文章适合老手
    Narwhal
        5
    Narwhal  
       May 26, 2018 via Android
    顺便把多服务器的 ssl 也配上?
    luguanyu1234
        6
    luguanyu1234  
       May 26, 2018
    @glues 哈哈哈哈哈哈哈
    qiuyk
        7
    qiuyk  
       May 26, 2018
    apache 是不是也得了解一下?
    Servo
        8
    Servo  
       May 26, 2018
    8102 年了。
    Weny
        9
    Weny  
       May 26, 2018
    张大妈既视感
    laudukang
        10
    laudukang  
       May 26, 2018
    大。。清什么时候
    Cambrian07
        11
    Cambrian07  
       May 26, 2018 via Android
    所以是跑在黑莓上的吗?
    moult
        12
    moult  
       May 26, 2018
    我刚学了 ASP,请问这个能运行 ASP 吗?
    fy
        13
    fy  
       May 26, 2018
    什么,现在已经是万历年间了????
    CEBBCAT
        14
    CEBBCAT  
       May 27, 2018
    ryd994
        15
    ryd994  
       May 27, 2018 via Android   ❤️ 1
    明明有发行版的二进制包,为什么总有人写教程如何编译?
    我要没什么需求,用得着自己编译?
    我要有什么需求,还轮得到你教?
    julyclyde
        16
    julyclyde  
       May 27, 2018
    @ryd994 为了凑篇幅

    其实大部分写编译文章的人都不清楚
    --with 各种、--enable 各种
    分别是什么意思,以及有哪些外部依赖
    junbaor
        17
    junbaor  
       May 27, 2018
    @ryd994 #15 有点道理哈哈
    adoal
        18
    adoal  
       May 27, 2018 via iPhone
    @julyclyde 一般也不知道 LSB、FHS,通常也不敢开 SELinux
    jerryshao
        19
    jerryshao  
       May 27, 2018
    看了一下 gRPC 框架实践那篇...代码风格不敢恭维
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2777 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 88ms · UTC 15:52 · PVG 23:52 · LAX 08:52 · JFK 11:52
    ♥ Do have faith in what you're doing.