Nginx 基础入门篇(2)

3.Nginx 部署-Yum

官网链接:http://www.nginx.org

 Nginx版本类型

Mainline version: 主线版,即开发版

Stable version: 最新稳定版,生产环境上建议使用的版本

Legacy versions: 遗留的老版本的稳定版

配置YUM源

已最新稳定版为例:1.18

去nginx官网下载

安装

环境问题

getenforce

systemctl status firewalld

yum -y install nginx     //刚刚配置文件的最后一步已经下载了

systemctl start nginx       // 启动程序

systemctl enable nginx  // 设置开机启动

nginx -V                //查看安装附带的功能模块

测试

4.Nginx 配置文件

所有文件

rpm -ql nginx     //查看所有nginx配置文件

/etc/logrotate.d/nginx                             日志轮转
/etc/nginx/nginx.con                              总配置文件
/etc/nginx/conf.d                                     子配置文件夹
/etc/nginx/conf.d/default.conf                 默认的网站配置文件

/etc/nginx/fastcgi_params                        动态网站模块文件-python,php所需的相关变量
/etc/nginx/scgi_params          
/etc/nginx/uwsgi_params
/etc/nginx/koi-utf                         字符集,文件编码
/etc/nginx/win-utf
/etc/nginx/koi-win
/etc/nginx/mime.types                  文件关联程序  网站文件类型 和 相关处理程序
/etc/nginx/modules                               模块文件夹。第三方模块
    
/etc/sysconfig/nginx
    # Configuration file for the nginx service.
    NGINX=/usr/sbin/nginx
    CONFFILE=/etc/nginx/nginx.conf
/etc/sysconfig/nginx-debug
    # Configuration file for the nginx-debug service.
    NGINX=/usr/sbin/nginx-debug
    CONFFILE=/etc/nginx/nginx.conf
    LOCKFILE=/var/lock/subsys/nginx-debug
/usr/lib/systemd/system/nginx-debug.service            nginx调试程序启动脚本
 
/usr/lib/systemd/system/nginx.service systemctl                服务脚本。
  
/usr/sbin/nginx                                                    主程序
 
/usr/sbin/nginx-debug                                       nginx调试程序
    
/usr/share/doc/nginx-1.12.1                文档
    
/usr/share/doc/nginx-1.12.1/COPYRIGHT
/usr/share/man/man8/nginx.8.gz                               man 手册
    
/usr/share/nginx
/usr/share/nginx/html
/usr/share/nginx/html/50x.html
/usr/share/nginx/html/index.html                        默认主页
  
/var/cache/nginx                                      缓存各种
  
    ls /var/cache/nginx/
        client_temp fastcgi_temp proxy_temp scgi_temp uwsgi_temp
/var/log/nginx                                                 日志文件夹
  
    ls /var/log/nginx/
        access.log error.log
/usr/lib64/nginx                                                          Nginx模块目录
     

5.Nginx 编译参数

nginx -V

基本配置

configure arguments:                           配置参数./configure --help查询帮助
  
--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                   程序ID
  
--lock-path=/var/run/nginx.lock                          锁路径,防止重复启动nginx
    
--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       php缓存
    
--http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp   python缓存 
    
--http-scgi-temp-path=/var/cache/nginx/scgi_temp 
--with-compat   启用动态模块兼容性
    
--user=nginx    用户
  
--group=nginx    组
  

模块类

--with-file-aio 

使用nginx的aio特性会大大提高性能,比如图片站的特点是大量的读io操作,nginx aio不用等待每次io的结果,有助于并发处理大量io和提高nginx处理效率。
aio的优点就是能够同时提交多个io请求给内核,然后直接由内核的io调度算法去处理这些请求(directio),这样的话,内核就有可能执行一些合并,节约了读取文件的处理时间。
就是异步非阻塞

--with-threads     多线程模块

--with-http_addition_module    //响应之前或者之后追加文本内容,比如想在站点底部追加一个js广告或者新增的css样式

--with-http_auth_request_module            认证模块
    
--with-http_dav_module         增加上传PUT,DELETE,MKCOL:创建集合,COPY和MOVE方法)默认情况下为关闭
    
--with-http_flv_module                     NGINX 添加MP4、FLV视频支持模块
  
--with-http_gunzip_module                 压缩模块
    
--with-http_gzip_static_module 
--with-http_mp4_module               多媒体模块
    
--with-http_random_index_module             nginx显示随机首页模块
    
--with-http_realip_module                         Nginx获取真实IP模块
 
--with-http_secure_link_module                     nginx安全下载模块
  
--with-http_slice_module                                   nginx中文文档
  
--with-http_ssl_module                   安全模块
    
--with-http_stub_status_module          访问状态
    
--with-http_sub_module                       nginx替换网站响应内容
    
--with-http_v2_module
--with-mail                             邮件客户端
    
--with-mail_ssl_module 
--with-stream                  负载均衡模块。nginx从1.9.0开始,新增加了一个stream模块,用来实现四层协议的转发、代理或者负载均衡等。
  
--with-stream_realip_module 
--with-stream_ssl_module
--with-stream_ssl_preread_module 

CPU优化参数

--with-stream_realip_module 
--with-stream_ssl_module
--with-stream_ssl_preread_module 
--with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --
param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' 
--with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie' 

6.Nginx 基本配置

观察主配置文件

分类

CoreModule 核心模块  (进程数等)

EventsModule 事件驱动模块(工作模式等)

HttpCoreModule        http内核模块(文档程序类型,配置文件等)

模块功能

     1、全局/核心块:配置影响nginx全局的指令。一般有运行nginx服务器的用户组,nginx进程pid存放路径,日志存放路径,配置文件引入,允许生成worker process数等。

    2、events块:配置影响nginx服务器或与用户的网络连接。有每个进程的最大连接数,选取哪种事件驱动模型处理连接请求,是否允许同时接受多个网路连接,开启多个网络连接序列化等。

    3、http块:可以嵌套多个server,配置代理,缓存,日志定义等绝大多数功能和第三方模块的配置。如文件引入,mime-type定义,日志自定义,是否使用sendfile传输文件,连接超时时间,单连接请求数等。

    4、server块:配置虚拟主机的相关参数,一个http中可以有多个server。

    5、location块:配置请求的路由,以及各种页面的处理情况。 

vim /etc/nginx/nginx.conf (主配置 )

user nginx;              运行nginx程序的独立账号

worker_processes 1;        启动的worker进程数量(CPU数量一致或auto)

error_log /var/log/nginx/error.log warn;             错误日志存放位置

pid /var/run/nginx.pid;

events {                    事件

use epoll;               事件驱动模型epoll【默认】 事件驱动模型分类,select|poll|kqueue|epoll|resig|/dev/poll|eventport

worker_connections 10240;             //每个worker进程允许处理的最大连接数,例如10240,65535

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;  

sendfile on;                       优化参数

高效传输文件的模式

Nginx高级篇sendfile配置
sendfile: 设置为on表示启动高效传输文件的模式。sendfile可以让Nginx在传输文件时直接在磁盘和tcp socket之间传输数据。如果这个参数不开启,会先在用户空间(Nginx进程空间)申请一个buffer,用read函数把数据从磁盘读到cache,再从cache读取到用户空间的buffer,再用write函数把数据从用户空间的buffer写入到内核的buffer,最后到tcp socket。开启这个参数后可以让数据不用经过用户buffer。

#tcp_nopush on;         优化参数

也就是说tcp_nopush = on 会设置调用tcp_cork方法,这个也是默认的,结果就是数据包不会马上传送出去,等到数据包最大时,一次性的传输出去,这样有助于解决网络堵塞。

keepalive_timeout 65;              优化参数    长连接

#gzip on;                                  压缩参数

include /etc/nginx/conf.d/*.conf;                       包含子配置文件夹

观察默认虚拟主机配置文件

vim /etc/nginx/conf.d/default.conf 

server {                                      默认网站配置文件
                                         
listen 80;                                         监听端口
  
server_name localhost;                    FQDN
    
#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          vim /etc/nginx/conf.d/abc.conf 

 

server     {
listen    80;
server_name    abc.com;
location / {
root    /abc;
index     index.html ;
}
}

server 虚拟主机
listen 监听端口
server_name 服务器名称
location 网站目录设置
root 网站主目录在本地的路径
index 主页文件名

http{} 是整个服务器,所有虚拟主机的设置。
server{}是某一个虚拟主机的设置
location{} 是某一个页面的设置。

mkdir   /abc

 touch /abc/index.html
echo   123  >  /abc/index.html

2 重启服务

systemctl restart nginx
 

3 域名解析和访问

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值