Nginx的配置与开发学习(一)

版权声明:转载记得宣传奥~。~ https://blog.csdn.net/c_ym_ww/article/details/87165865

确认工作

  • ping www.baidu.com
  • 确认yum源是否可用 yum list|grep gcc
  • 关闭iptables配置 iptables具体用法
  • iptables -L(查看是否有iptables规则)
  • iptables -F
  • iptables -t nat -L
  • iptables -t nat -F
  • 关闭SELinex
  • getenforce 查看是否关闭 没关闭的话就 setenforce 0
  • 安装包 yum -y install gcc gcc-c++ autoconf pcre pcre-devel make automake
  • 安装工具包 yum -y install wget httpd-tools vim
  • 到相关目录 cd /opt/ 建立文件下mkdir XX XX XX XX
    所以删除文件夹是 使用rm -rf 目录名字
    -r 就是向下递归,不管有多少级目录,一并删除
    -f 就是直接强行删除,不作任何提示的意思

Nginx简述

Nginx是一个开源且高性能,可靠的HTTP中间件,代理服务

Nginx优势

  1. IO多路复用epoll(老师出题给多个学生做,老师一个个去问或者许多老师监控每一个学生不如 学生主动上报)

​ 多个描述符的I/O操作都能在一个线程内并发交替地顺序完成,这就叫I/O多路复用,复用指的是复用同一个线程。

  1. 轻量级 足够轻量级的Web服务

    功能模块少 只放核心代码,插件代码不会放 因此舍弃功能而注重性能

    代码模块化 易读,可以进行二次的改进

  2. CPU亲和(affinity)

    是一种把CPU核心和Nginx工作进程绑定方式,把每一个worker进程固定在一个cpu上执行,减少切换cpu的cache miss,获得更好的性能。

  3. sendfile

    file要经过内核—>用户空间–>通信,sendfile只通过内核–>通信了

Nginx安装

  1. 登录网站

  2. Pre-Built Packages for Stable version
    To set up the yum repository for RHEL/CentOS, create the file named /etc/yum.repos.d/nginx.repo with the following contents:
    
    [nginx]
    name=nginx repo
    baseurl=http://nginx.org/packages/OS/OSRELEASE/$basearch/
    gpgcheck=0
    enabled=1
    
    Replace “OS” with “rhel” or “centos”, depending on the distribution used, and “OSRELEASE” with “6” or “7”, for 6.x or 7.x versions, respectively.
    
    上面的注解特别重要所以实际上是在vim /etc/yum.repos.d/nginx.repo
    [nginx]
    name=nginx repo
    baseurl=http://nginx.org/packages/centos/7/$basearch/
    gpgcheck=0
    enabled=1
    
  3. yum list|grep nginx 查看

  4. yum install nginx 安装nginx

  5. nginx -v 查看nginx版本号

安装参数

命令: rpm -ql nginx 在操作系统上的相关配置文件

MIME(Multipurpose Internet Mail Extension,多用途因特网邮件拓展)

路径 类型 作用
/etc/logrotate.d/nginx 配置文件 Nginx日志轮转,用于logrotate服务的日志切割
/etc/nginx,
/etc/nginx/nginx.conf,
/etc/nginx/conf.d,
/etc/nginx/conf.d/default.conf
目录,配置文件 Nginx主配置文件
/etc/nginx/fastcgi_params,
/etc/nginx/uwscgi_params,
/etc/nginx/scgi_params
配置文件 cgi配置相关,fastcgi
/etc/nginx/koi-utf,
/etc/nginx/koi-win,
/etc/nginx/win-utf
配置文件 编码转换映射转化文件
/etc/nginx/mime.types 配置文件 设置http协议的Content-Type与拓展名对应关系
/usr/lib/systemd/system/nginx-debug.service,
/usr/lib/systemd/system/nginx.service,
/etc/sysconfig/nginx,
/etc/sysconfig/nginx-debug
配置文件 用于配置出系统守护进程管理器管理方式
/usr/lib64/nginx/modules,
/etc/nginx/modules
目录 Nginx模块目录
/usr/sbin/nginx,
/usr/sbin/nginx-debug
命令 Nginx服务的启动管理的终端命令

| /usr/share/doc/nginx-1.15.5
/usr/share/doc/nginx-1.15.5/COPYRIGHT
/usr/share/man/man8/nginx.8.gz | 文件,目录 | Nginx的手册和帮助文件 |
| /var/cache/nginx | 目录 | Nginx的缓存目录 |
| /var/log/nginx | 目录 | Nginx的日志目录 |

命令: nginx -V 显示安装编译参数

编译选项 作用
–prefix=/etc/nginx
–sbin-path=/usr/sbin/nginx
–modules-path=/usr/lib64/nginx/modules
–conf-path=/etc/nginx/nginx.conf
–error-log-path=/var/log/nginx/error.log
–http-log-path=/var/log/nginx/access.log
–pid-path=/var/run/nginx.pid
–lock-path=/var/run/nginx.lock
安装目的目录或路径
–http-client-body-temp-path=/var/cache/nginx/client_temp
–http-proxy-temp-path=/var/cache/nginx/proxy_temp
–http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp
–http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp
–http-scgi-temp-path=/var/cache/nginx/scgi_temp
执行对应模块时,Nginx所保留的临时性文件(临时性文件,不重要的文件)
–user=nginx
–group=nginx
设置Nginx进程启动的用户和组用户
–with-cc-opt=parameters 设置额外的参数将被添加到CFLAGS变量

Centos操作

  1. cd /etc/nginx/ 到nginx目录下

  2. vim nginx.conf

    user  nginx;    //设置nginx服务的系统使用用户
    worker_processes  1;   //工作进程数,跟CPU数保持一致
    
    error_log  /var/log/nginx/error.log warn;  //nginx的错误日志  日志级别
    pid        /var/run/nginx.pid;            //nginx服务启动时候pid
    
    
    events {
        worker_connections  1024;  //每个进程允许最大连接数
    }
    
    
    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;  //main表示main的格式进入access.log
    
        sendfile        on;
        #tcp_nopush     on;
    
        keepalive_timeout  65;
    
        #gzip  on;
    
        include /etc/nginx/conf.d/*.conf;
    }
    
    
  3. cat conf.d/default.conf

    server {
        listen       80;  //确保端口不被占用
        server_name  localhost;
    
        #charset koi8-r;
        #access_log  /var/log/nginx/host.access.log  main;
    
        location / {
            root   /usr/share/nginx/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   /usr/share/nginx/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;
        #}
    }
    
    
    1. 重启nginx systemctl restart nginx.service

Nginx变量

  1. HTTP请求变量-arg_PARAMETER,http_HEADER,sent_http_HEADER
  2. 内置变量 -Nginx内置的
  3. 自定义变量-自己定义 更多参数配置可以看一下 [Nginx官网](

猜你喜欢

转载自blog.csdn.net/c_ym_ww/article/details/87165865