Skip to content

Nginx学习(第一部分) #50

Open
@huangchucai

Description

@huangchucai

nginx学习

环境配置

  1. linux系统检查
   1. ping www.baidu.com. //检查网络
   2. yum list|grep gcc  //列出gcc的部分
   3. intables关闭  
      iptables -L  查看是否开启
      iptables -S  关闭
   4. getenforce 关闭
  1. 安装依赖

    1. yum -y install gcc gcc-c++ autoconf pcre-devel make automake
    2.yum -y install wget httpd-tools vim  (httpd-tools加密的)   
  2. 创建工作目录

    1. cd /opt/
    2. mkdir app backup download logs work
    // 1. backup 放备用文件 2. app 当代码 3. download存放下载的内容 4. logs 存放日志 5. work存放脚本代码
    
    
  3. nginx安装

    Mainline version(开发版本)。Stable version(稳定版本) Legacy version(过去版本)

    • https://nginx.org/en/download.html 下载地址

    • Linux centos

      • Pre-Built Packages 寻找稳定版本

        [nginx]
        name=nginx repo
        baseurl=http://nginx.org/packages/centos/7/$basearch/
        gpgcheck=0
        enabled=1
      • 配置信息

        // 1. cd /etc/yum.repos.d
        // 2. vim nginx.repo
        // 3. 复制上面的代码,保存并退出
      • 查看源信息

        yum list|grep nginx
      • 安装

        yum install nginx
        
    • 查看文件的存放目录和安装路径

      rpm -ql nginx
  4. 工具

    // 查看后台进程
    1. ps -aux|grep nginx
    // 查看本机因为Nginx进程所启用的端口
    2. netstat -luntp|grep nginx

nginx的中间件架构

1. 安装目录讲解(rpm -ql nginx)

  1. 配置文件

    • /etc/logrotate.d/nginx
    • 作用:Nginx日志轮转,用于logrotate服务的日志切割
  2. 目录和配置文件

    • /etc/nginx /etc/nginx/nginx.conf
      /etc/nginx/conf.d /etc/nginx/conf.d/default.conf (核心配置)

    • 作用: Nginx主配置文件 先走Nginx的配置文件,如果我们没有配置,就走default.conf默认配置文件

    • 保存和检查配置

      1. nginx -t -c /etc/nginx/nginx.conf  
      // -t 表示配置文件的语法检测  -c 配置文件测试是否成功
      2. nginx -s reload -c /etc/nginx/nginx.conf
      // 重启 并制定配置
      3. ps -aux|grep nginx
  3. http协议的关系

    • /etc/nginx/mime.types
    • 作用:设置http协议的Content-Type与扩展名对应关系(不能识别的额外添加)
  4. 执行命令

    • /usr/sbin/nginx(主要这个) /usr/sbin/nginx-debug

    • 作用:Nginx服务的启动管理的终端命令(用于调试和分析)

    • nginx -tc /etc/nginx/nginx.conf (检查配置项)

    • nginx -c /etc/nginx/nginx.conf / systemctl start nginx.service (开启nginx)

    • nginx -s reload -c /etc/nginx/nginx.conf / systemctl reload nginx.service (重启Nginx服务)

    • nginx -s stop -c /etc/nginx/nginx.conf / systemctl stop nginx.service (关闭服务)

  5. 缓存

    • /var/cache/nginx
    • 作用: 用于启动服务后的缓存
  6. 日志

    • /var/log/nginx
    • 作用: 存放Nginx的日志
    • 查看日志
    1. tail -n 200 /var/log/nginx/access.log (展示最近200条)
    2. tail -f /var/log/nginx/error.log (一直读取日志)

2. 安装编译参数(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进程启动的用户和组用户

3. Nginx的主要配置

1. cd /etc/nginx
2. vi nginx.conf
3.1: nginx.conf中的http部分
http {
    // 设置了Nginx中所有的Content-Type类型
    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;
       // 读取默认的配置
    include /etc/nginx/conf.d/*.conf;
    sendfile        on;
    #tcp_nopush     on;
	// 客户端和服务端的连接时间
    keepalive_timeout  65;

    #gzip  on;
    server {
        listen: 80;
        location {
            proxy_pass http://localhost:1234
            proxy_redirect off;
            proxy_set_header Host $host;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Real-IP $remote_addr;
        }
    }

如果没有配置server,就会走defalut.conf的文件(/etc/nginx/conf.d/default.conf)

server {
    // 对于不同的server,可以根据listen(端口),也可以根据server_name区别
    listen       80;
    server_name  localhost;

    #charset koi8-r;    #access_log  /var/log/nginx/host.access.log  main;
  // 如果开启了nginx,路径:/  目录:/usr/share/nginx/html  文件: index.html index.htm
    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
    #
    // 如果是500等服务器错误,就走/50.html 
    error_page   500 502 503 504  /50x.html;
    // 路径: /50.html  文件的存放目录: /usr/share/nginx/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;
    #}
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions