zoukankan      html  css  js  c++  java
  • centOS7.*安装nginx和简单使用

    安装nginx

    1. 官网下载对应的nginx包,推荐使用稳定版本。
    2. 上传下载好的包到服务器
    3. 安装依赖环境
      1. 安装gcc环境。
        yum install gcc-c++
      2. 安装PCRE库,用于解析正则表达式。
        yum install -y pcre pcre-devel
      3. zlib压缩和解压缩依赖。
        yum install -y zlib zlib-devel
      4. SSL 安全的加密的套接字协议层,用于HTTP安全传输,也就是https。
        yum install -y openssl openssl-devel
    4. 解压缩,解压后得到的是源码,需要对源码进行编译后才可以安装
      tar -zxvf nginx-1.16.1.tar.gz
    5. 编译之前先创建临时目录,如果不创建,在启动过程中将会报错
      mkdir /var/temp/nginx -p
    6. 进入到nginx解压缩后的目录,输入如下命令进行配置,目的是为了创建makefile文件
      ./configure \ --prefix=/usr/local/nginx \ --pid-path=/var/run/nginx/nginx.pid \ --lock-path=/var/lock/nginx.lock \ --error-log-path=/var/log/nginx/error.log \ --http-log-path=/var/log/nginx/access.log \ --with-http_gzip_static_module \ --http-client-body-temp-path=/var/temp/nginx/client \ --http-proxy-temp-path=/var/temp/nginx/proxy \ --http-fastcgi-temp-path=/var/temp/nginx/fastcgi \ --http-uwsgi-temp-path=/var/temp/nginx/uwsgi \ --http-scgi-temp-path=/var/temp/nginx/scgi

      配置命令参数详解:

    7. make编译、安装

      make

      make install

    8. 进入sbin目录启动nginx

      ./nginx
      
      * 停止:./nginx -s stop
      * 重新加载:./nginx -s reload
    9. 打开浏览器,访问虚拟机所处内网ip即可打开nginx默认页面。
    10. 在本地虚拟机进行操作,记得关闭防火墙。在云服务器进行操作,记得开放80端口。

    简单使用

    1. nginx常用命令:
      查看nginx版本信息:
      #简略信息,只显示版本号
      [root@kevin sbin]# ./nginx -v
      nginx version: nginx/1.16.1
      
      #详细信息,包括版本号,编译版本/工具(GCC),配置参数(configure arguments)。
      [root@kevin sbin]# ./nginx -V
      nginx version: nginx/1.16.1
      built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC)
      configure arguments: --prefix=/usr/local/nginx --pid-path=/var/run/nginx/nginx.pid --lock-path=/var/lock/nginx.lock --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --with-http_gzip_static_module --http-client-body-temp-path=/var/temp/nginx/client --http-proxy-temp-path=/var/temp/nginx/proxy --http-fastcgi-temp-path=/var/temp/nginx/fastcgi --http-uwsgi-temp-path=/var/temp/nginx/uwsgi --http-scgi-temp-path=/var/temp/nginx/scgi
      测试nginx配置是否正确:
      [root@kevin sbin]# ./nginx -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
      
      #./nginx -T 将会显示详细的配置信息(include文件也会显示)
      启动、停止和重新启动nginx:
      #启动
      [root@kevin sbin]# ./nginx
      
      #停止-方式一,强制停止,当前有正在处理的请求也会被关闭掉。类似于饭店要关门,即使还有客人在吃饭,直接将其赶出去。
      [root@kevin sbin]# ./nginx -s stop
      
      #停止-方式二,不再接受新的请求,处理完当前正在执行的请求后关闭。类似于饭店要关门,会等到当前店内客人吃完饭之后关门,同时不再招待新的客人。
      [root@kevin sbin]# ./nginx -s quit
      
      #重新启动/加载。当修改了配置文件后,使用该命令进行重载。
      [root@kevin sbin]# ./nginx -s reload 
    2. 如果报错了——启动失败,找不到pid
      1. 方式一,检查文件目录是否存在,不存在则创建
      2. 方式二,进入到sbin目录下,使用“./nginx -h”查看帮助文档。使用命令“./nginx -c /usr/local/nginx/conf/nginx.conf”重新指定conf文件路径
    3. nginx配置文件的结构
      1. 文件内容本身是一个main 全局配置。一条指令以";"为结束符,一个指令块以"{}"作为起止符。
    4. nginx配置文件中server的location匹配规则
      1. 空格:默认匹配,普通匹配
        location / {
          root /home;
        }
      2. =:精确匹配
        location = /imooc/img/face1.png {
            root /home;
        }
      3. ~*:匹配正则表达式,不区分大小写
        #符合图片的显示
        location ~* \.(GIF|jpg|png|jpeg) {
            root /home;
        }
      4. ~:匹配正则表达式,区分大小写
        #GIF必须大写才能匹配到
        location ~ \.(GIF|jpg|png|jpeg) {
            root /home;
        }
      5. ^~:以某个字符路径开头
        location ^~ /imooc/img {
            root /home;
        }
    如有错误,恳请指出。
  • 相关阅读:
    【转】三层架构,MVC, ASP.net MVC的区别
    code-Behind
    从输入 URL 到页面加载完成的过程中都发生了什么事情?
    javascript杂谈
    网页设计中透明效果的使用技巧
    phpstorm+Xdebug断点调试PHP
    MySql IFNULL 联表查询出来的null 如何赋值
    php读取目录下的文件
    CI框架程序--本地调试之后部署新浪SAE
    各个手机APP客户端内置浏览器useragent
  • 原文地址:https://www.cnblogs.com/monument/p/12737929.html
Copyright © 2011-2022 走看看