前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Nginx 基础篇

Nginx 基础篇

作者头像
用户1456517
发布2019-03-05 16:02:07
4360
发布2019-03-05 16:02:07
举报
文章被收录于专栏:芝麻实验室芝麻实验室
安装Nginx
  • Nginx被收录在EPEL Project中,因此安装Nginx前需先安装EPEL仓库
代码语言:javascript
复制
# yum -y install epel-release
# yum -y install nginx
  • 通过tree命令查看Nginx主体配置结构
代码语言:javascript
复制
# tree /etc/nginx/
.
├── conf.d                  #辅助配置文件
├── default.d               #通用(默认)配置文件
├── fastcgi.conf            #fastcgi相关变量配置文件,一般作用于以PHP-FPM为后端解释器场景,下同
├── fastcgi.conf.default #
├── fastcgi_params          #fastcgi相关变量配置文件,一般作用于以PHP-FPM为后端解释器场景,下同
├── fastcgi_params.default
├── koi-utf
├── koi-win
├── mime.types              #gzip文件压缩类型对照表,作用于显式开启gzip功能时;是系统自带的 /etc/mime.types 文件的子集
├── mime.types.default
├── nginx.conf              #Nginx主配置文件
├── nginx.conf.default
├── scgi_params             #scgi相关变量配置文件,下同
├── scgi_params.default
├── uwsgi_params            #uwsgi #uwsgi相关变量配置文件,一般同基于Python的Web框架(eg: Flask/Django)协同工作,下同
├── uwsgi_params.default
└── win-utf

2 directories, 15 files
Nginx主配置文件详解
代码语言:javascript
复制
[root@Center nginx]# less nginx.conf
# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/

user nginx; # 以指定用户身份运行worker process(工作进程)
worker_processes auto; # worker process数量,建议 ≤ CPU核心数,auto表示等于CPU核心数
error_log /var/log/nginx/error.log; #错误日志存储路径
pid /run/nginx.pid; #主进程文件目录(PID文件)

# Load dynamic modules. See /usr/share/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf; #动态模块配置文件

events {
    worker_connections 1024; #每个worker process所能够接受的最大并发连接数,建议使用默认值
}

http {
    #访问日志格式定义
    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; #应用特定的访问日志格式,并指明存储路径

    sendfile            on; #开启sendfile功能
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65; #长连接超时时长
    types_hash_max_size 2048;
    gzip  on;               #开启gzip压缩功能

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf; #引用辅助配置文件

    server { #定义一个虚拟主机
        listen       80 default_server; #监听服务端口(IPv4),default_server表示当无匹配的server时,当前server为默认响应server(注:1个Nginx实例仅允许存在1个default_server声明)
        listen       [::]:80 default_server; #同上,此处为IPv6相关配置
        server_name  t.zhimajihua.cn; #域名
        root         /usr/share/nginx/html; #站点根目录

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf; #引用通用(默认)配置文件

        location / { #定义location
            root   html; #匹配根目录时,站点主目录路径
            index  index.html index.htm; #默认首页名称,从左到右匹配
        }

# proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #当用户请求任意以 .php 为后缀的页面时,代理到特定后端主机;也可以应用在HTTPS配置段
        location ~ \.php$ {
            proxy_pass   http://127.0.0.1; #反代到特定后端主机,格式形如:proxy_pass http://IP:PORT;
        }

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #fastcgi反代;也可以应用在HTTPS配置段
        location ~ \.php$ {
            root           html;
            fastcgi_pass   127.0.0.1:9000; #处理fastcgi请求的后端主机地址
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name; #该处指明的路径将用于匹配 require_url 的相对路径,/scripts应替换为站点根目录路径或$document_root(推荐)
            include        fastcgi_params;
        }

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}

        error_page 404 /404.html; #定义匹配特定错误码时,错误页路径,下同
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }

# HTTPS相关配置段
# Settings for a TLS enabled server.
#
#    server { 
#        listen       443 ssl http2 default_server; #监听本地443(IPv4)端口,使用SSL/TLS协议加密,启用http2协议(如客户端不支持将自动向前适应 HTTP/1.1 )
#        listen       [::]:443 ssl http2 default_server; #IPv6相关
#        server_name  t.zhimajihua.cn;
#        root         /usr/share/nginx/html;
#
#        ssl_certificate "/etc/pki/nginx/server.crt"; #SSL证书路径
#        ssl_certificate_key "/etc/pki/nginx/private/server.key"; #SSL证书私钥路径
#        ssl_session_cache shared:SSL:1m; #SSL会话缓存配置
#        ssl_session_timeout  10m; #SSL会话超时时长,默认10min
#        ssl_ciphers HIGH:!aNULL:!MD5;
#        ssl_prefer_server_ciphers on;
#
#        # Load configuration files for the default server block.
#        include /etc/nginx/default.d/*.conf;
#
#        location / {
             root    /usr/share/nginx/html;
#        }
#
#        error_page 404 /404.html;
#            location = /40x.html {
#        }
#
#        error_page 500 502 503 504 /50x.html;
#            location = /50x.html {
#        }
#    }

}

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018/02/11,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 安装Nginx
  • Nginx主配置文件详解
相关产品与服务
SSL 证书
腾讯云 SSL 证书(SSL Certificates)为您提供 SSL 证书的申请、管理、部署等服务,为您提供一站式 HTTPS 解决方案。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档