Nginx 核心配置-新建一个web站点
Nginx核心配置-新建一个web站点
作者:尹正杰
版权声明:原创作品,谢绝转载!否则将追究法律责任。
一.Nginx基础配置常用参数说明
[root@node101.yinzhengjie.org.cn ~]# cat /yinzhengjie/softwares/nginx/conf/nginx.conf
#启动Nginx工作进程的用户和组,比如"user nginx nginx"
#user nobody; #启动nginx工作进程的数量,默认是一个,这个参数取决于你服务器CPU的核心数,一般情况下该值要小于等于你服务器的核心数哟。在nginx1.8版本之后咱们可以直接写"auto",即无需手写几个core数量,而是交给nginx自动取判断服务器拥有的core数量。
worker_processes ; #将Nginx工作进程绑定到指定的CPU核心,默认Nginx是不进行进程绑定的,绑定并不是意味着当前nginx进程独占以一核心CPU,但是可以保证此进程不会运行在其他核心上,这就极大减少了nginx的工作进程在不同的cpu核心上的来回跳转,减少了CPU对进程的资源分配与回收以及内存管理等,因此可以有效的提升nginx服务器的性能。
worker_cpu_affinity ; #错误日志记录配置,语法:error_log file [debug | info | notice | warn | error | crit |alert | emerg]
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info; #pid文件保存路径
#pid logs/nginx.pid; #工作进程优先级,-~
worker_priority ; #这个数字包括Nginx的所有连接(例如与代理服务器的连接等),而不仅仅是与客户端的连接,另一个考虑因素是实际的并发连接数不能超过系统级别的最大打开文件数的限制.
worker_rlimit_nofile ; #前台运行Nginx服务用于测试、docker等环境。
daemon off; #是否开启Nginx的master-woker工作模式。
master_process off|on; #events设置快,主要影响nginx服务器与用户的网络连接,比如是否允许同时接受多个网络连接,使用哪种事件驱动模型处理请求,每个工作进程可以同时支持的最大连接数,是否开启对多工作进程下的网络连接进行序列化等。
events { #设置单个nginx工作进程可以接受的最大并发,作为web服务器的时候最大并发数为"worker_connections * worker_processes",作为反向代理的时候为"(worker_connections * worker_processes)/2"(因为反向代理服务器是响应一个客户端请求连接需要消耗两个文件描述符,即接收客户端请求需要消耗一个文件描述符,将请求转发给后端的rip服务器处理又消费了一个文件描述符)
worker_connections ; #使用epoll事件驱动,Nginx支持众多的事件驱动,比如select、poll、epoll,只能设置在events模块中设置。
use epoll; #优化同一时刻只有一个请求而避免多个睡眠进程被唤醒的设置,on为防止被同时唤醒默认为off,全部唤醒的过程也成为"惊群",因此nginx刚安装完以后要进行适当的优化。
accept_mutex on; #Nginx服务器的每个工作进程可以同时接受多个新的网络连接,但是需要在配置文件中配置,此指令默认为关闭,即默认为一个工作进程只能一次接受一个新的网络连接,打开后几个同时接受多个,配置语法如下:
multi_accept on; } #http块是Nginx服务器配置中的重要部分,缓存、代理和日志格式定义等绝大多数功能和第三方模块都可以在这设置,http块可以包含多个server块,而一个server块中又可以包含多个location块,server块可以配置文件引入、MIME-Type定义、日志自定义、是否启用sendfile、连接超时时间和单个链接的请求上限等。
http { #导入支持的文件类型,mime.types:支持的mime类型,MIME(Multipurpose Internet Mail Extensions)多用途互联网邮件扩展类型,MIME消息能包含文本、图像、音频、视频以及其他应用程序专用的数据,是设定某种扩展名的文件用一种应用程序来打开的方式类型,当该扩展名文件被访问的时候,浏览器会自动使用指定应用程序来打开。多用于指定一些客户端自定义的文件名,以及一些媒体文件打开方式。
include 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 logs/access.log main; #指定是否使用sendfile系统调用来传输文件,sendfile系统调用在两个文件描述符之间直接传递数据(完全在内核中操作),从而避免了数据在内核缓冲区和用户缓冲区之间的拷贝,操作效率很高,被称之为零拷贝,硬盘 >> kernel buffer (快速拷贝到kernelsocket buffer) >>协议栈。作为web服务器的时候我推荐大家打开sendfile加快文件传输
sendfile on; #在开启了sendfile的情况下,合并请求后统一发送给客户端。
#tcp_nopush on; #在开启了keepalived模式下的连接是否启用TCP_NODELAY选项,当为off时,延迟0.2s发送,默认On时,不延迟发送,立即发送用户相应报文。
#tcp_nodelay off; #长连接超时时间,单位是秒,即设置会话保持时间
#keepalive_timeout ;
#设置会话保持时间时,我们也可以指定2个参数,第一个参数表示会话的保存时间,第二个参数是nginx通过response报文告诉客户端会话的保持时间,比如咱们配置的了服务端主动告诉客户端设置的连接超时时间是60秒,而实际上是65秒会话才会断开哟~
keepalive_timeout ;
#通常情况下咱们都会开启压缩功能的哟~
#gzip on; #设置一个虚拟机主机,可以包含自己的全局快,同时也可以包含多个locating模块。比如本虚拟机监听的端口、本虚拟机的名称和IP配置,多个server 可以使用一个端口,比如都使用80端口提供web服务、
server { #配置server监听的端口
listen ; #本server的名称,当访问此名称的时候nginx会调用当前serevr内部的配置进程匹配,以空格方式隔开多个FQDN,当然也支持正则表达式的方式匹配主机名。
server_name localhost;
#指定编码格式,推荐修改为UTF-8字符编码
#charset koi8-r; #access_log logs/host.access.log main; #location其实是server的一个指令,为nginx服务器提供比较多而且灵活的指令,都是在location中提现的,主要是基于nginx接受到的请求字符串,对用户请求的UIL进行匹配,并对特定的指令进行处理,包括地址重定向、数据缓存和应答控制等功能都是在这部分实现,另外很多第三方模块的配置也是在location模块中配置。
location / { #相当于默认页面的目录名称,默认是相对路径(如果是基于yum方式安装则是"/usr/share/nginx/html/",如果是基于源码方式安装,则在安装目录的下,如"/yinzhengjie/softwares/nginx/html/"),当然咱们也可以使用绝对路径配置。
root html; #默认的页面文件名称
index index.html index.htm;
} #错误页面的文件名称
#error_page /.html;
# redirect server error pages to the static page /50x.html
#
error_page /50x.html; #location处理对应的不同错误码的页面定义到/50x.html,这个跟对应其server中定义的目录下。
location = /50x.html { #定义默认页面所在的目录
root html;
} #以http的方式转发php请求到指定web服务器
# proxy the PHP scripts to Apache listening on 127.0.0.1:
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#} #以fastcgi的方式转发php请求到php处理
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:;
# 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 {
#拒绝web形式访问指定文件,如很多的网站都是通过.htaccess文件来改变自己的重定向等功能。
# deny all;
#}
} #自定义虚拟server
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen ;
# listen somename:;
# server_name somename alias another.alias; # location / {
# root html;
#指定默认网页文件,此指令由ngx_http_index_module模块提供
# index index.html index.htm;
# }
#} #https服务器配置
# HTTPS server
#
#server {
# listen ssl;
# server_name localhost; # ssl_certificate cert.pem;
# ssl_certificate_key cert.key; # ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m; # ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on; # location / {
# root html;
# index index.html index.htm;
# }
#} #和邮件相关的配置
#mail {
# ... #mail 协议相关配置段
# } #tcp代理配置,.9版本以上支持
#stream {
# ... #stream 服务器相关配置段
# } #导入其他路径的配置文件
#include /yinzhengjie/softwares/nginx/conf.d/*.conf; }
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# ps axo pid,cmd,psr | grep nginx #如下所示,使用worker_cpu_affinity关键词可以查看worker进程被绑定到的CPU所对应的core编号。
nginx: master process nginx
nginx: worker process
nginx: worker process
nginx: worker process
nginx: worker process
grep --color=auto nginx
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# ps axo pid,cmd,psr | grep nginx #如下所示,使用worker_cpu_affinity关键词可以查看worker进程被绑定到的CPU所对应的core编号。
[root@node101.yinzhengjie.org.cn ~]# cat /yinzhengjie/softwares/nginx/conf/mime.types #查看Nginx内部定义支持的MIME类型 types {
text/html html htm shtml;
text/css css;
text/xml xml;
image/gif gif;
image/jpeg jpeg jpg;
application/javascript js;
application/atom+xml atom;
application/rss+xml rss; text/mathml mml;
text/plain txt;
text/vnd.sun.j2me.app-descriptor jad;
text/vnd.wap.wml wml;
text/x-component htc; image/png png;
image/svg+xml svg svgz;
image/tiff tif tiff;
image/vnd.wap.wbmp wbmp;
image/webp webp;
image/x-icon ico;
image/x-jng jng;
image/x-ms-bmp bmp; application/font-woff woff;
application/java-archive jar war ear;
application/json json;
application/mac-binhex40 hqx;
application/msword doc;
application/pdf pdf;
application/postscript ps eps ai;
application/rtf rtf;
application/vnd.apple.mpegurl m3u8;
application/vnd.google-earth.kml+xml kml;
application/vnd.google-earth.kmz kmz;
application/vnd.ms-excel xls;
application/vnd.ms-fontobject eot;
application/vnd.ms-powerpoint ppt;
application/vnd.oasis.opendocument.graphics odg;
application/vnd.oasis.opendocument.presentation odp;
application/vnd.oasis.opendocument.spreadsheet ods;
application/vnd.oasis.opendocument.text odt;
application/vnd.openxmlformats-officedocument.presentationml.presentation
pptx;
application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
xlsx;
application/vnd.openxmlformats-officedocument.wordprocessingml.document
docx;
application/vnd.wap.wmlc wmlc;
application/x-7z-compressed 7z;
application/x-cocoa cco;
application/x-java-archive-diff jardiff;
application/x-java-jnlp-file jnlp;
application/x-makeself run;
application/x-perl pl pm;
application/x-pilot prc pdb;
application/x-rar-compressed rar;
application/x-redhat-package-manager rpm;
application/x-sea sea;
application/x-shockwave-flash swf;
application/x-stuffit sit;
application/x-tcl tcl tk;
application/x-x509-ca-cert der pem crt;
application/x-xpinstall xpi;
application/xhtml+xml xhtml;
application/xspf+xml xspf;
application/zip zip; application/octet-stream bin exe dll;
application/octet-stream deb;
application/octet-stream dmg;
application/octet-stream iso img;
application/octet-stream msi msp msm; audio/midi mid midi kar;
audio/mpeg mp3;
audio/ogg ogg;
audio/x-m4a m4a;
audio/x-realaudio ra; video/3gpp 3gpp 3gp;
video/mp2t ts;
video/mp4 mp4;
video/mpeg mpeg mpg;
video/quicktime mov;
video/webm webm;
video/x-flv flv;
video/x-m4v m4v;
video/x-mng mng;
video/x-ms-asf asx asf;
video/x-ms-wmv wmv;
video/x-msvideo avi;
}
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# cat /yinzhengjie/softwares/nginx/conf/mime.types #查看Nginx内部定义支持的MIME类型
Nginx的配置文件的组成部分:
主配置文件:nginx.conf
子配置文件 include conf.d/*.conf Nginx主配置文件的配置指令方式:
directive value [value2 ...];
注意:
(1) 指令必须以分号结尾
(2) 支持使用配置变量
内建变量:由Nginx模块引入,可直接引用
自定义变量:由用户使用set命令定义
set variable_name value;
引用变量:$variable_name MIME参考文档:https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Basics_of_HTTP/MIME_Types
二.Nginx新建一个web站点案例实战
1>.编辑主配置文件并检查语法格式是否正确
[root@node101.yinzhengjie.org.cn ~]# cat /yinzhengjie/softwares/nginx/conf/nginx.conf
worker_processes ;
worker_cpu_affinity ; events {
worker_connections ;
use epoll;
accept_mutex on;
multi_accept on;
} http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout ;
server {
listen ;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
error_page /50x.html;
location = /50x.html {
root html;
}
} #导入其他路径的配置文件
include /yinzhengjie/softwares/nginx/conf.d/*.conf;
} [root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# nginx -t
nginx: the configuration file /yinzhengjie/softwares/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /yinzhengjie/softwares/nginx/conf/nginx.conf test is successful
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]#
2>.在配置子目录中编写配置文件并检查语法
[root@node101.yinzhengjie.org.cn ~]# mkdir -pv /yinzhengjie/softwares/nginx/conf.d
mkdir: created directory ‘/yinzhengjie/softwares/nginx/conf.d’
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# vim /yinzhengjie/softwares/nginx/conf.d/web.conf
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# cat /yinzhengjie/softwares/nginx/conf.d/web.conf
server {
listen ;
server_name node101.yinzhengjie.org.cn; location / {
root /yinzhengjie/data/web/nginx/html/java;
index index.html;
} location /python {
root /yinzhengjie/data/web/nginx/html;
index index.html;
} location /golang {
root /yinzhengjie/data/web/nginx/html;
index index.html;
} }
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# nginx -t
nginx: the configuration file /yinzhengjie/softwares/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /yinzhengjie/softwares/nginx/conf/nginx.conf test is successful
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]#
3>.创建测试数据
[root@node101.yinzhengjie.org.cn ~]# mkdir -pv /yinzhengjie/data/web/nginx/html/{java,python,golang}
mkdir: created directory ‘/yinzhengjie/data’
mkdir: created directory ‘/yinzhengjie/data/web’
mkdir: created directory ‘/yinzhengjie/data/web/nginx’
mkdir: created directory ‘/yinzhengjie/data/web/nginx/html’
mkdir: created directory ‘/yinzhengjie/data/web/nginx/html/java’
mkdir: created directory ‘/yinzhengjie/data/web/nginx/html/python’
mkdir: created directory ‘/yinzhengjie/data/web/nginx/html/golang’
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# echo "<h1 style='color:rgb(255,0,0)'>Java</h1>" > /yinzhengjie/data/web/nginx/html/java/index.html
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# echo "<h1 style='color:rgb(0,255,0)'>Python</h1>" > /yinzhengjie/data/web/nginx/html/python/index.html
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# echo "<h1 style='color:rgb(0,0,255)'>Golang</h1>" > /yinzhengjie/data/web/nginx/html/golang/index.html
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# ll /yinzhengjie/data/web/nginx/html/ -R
/yinzhengjie/data/web/nginx/html/:
total
drwxr-xr-x root root Dec : golang
drwxr-xr-x root root Dec : java
drwxr-xr-x root root Dec : python /yinzhengjie/data/web/nginx/html/golang:
total
-rw-r--r-- root root Dec : index.html /yinzhengjie/data/web/nginx/html/java:
total
-rw-r--r-- root root Dec : index.html /yinzhengjie/data/web/nginx/html/python:
total
-rw-r--r-- root root Dec : index.html
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]#
4>.启动nginx服务
[root@node101.yinzhengjie.org.cn ~]# grep 172.30.1.101 /etc/hosts
172.30.1.101 node101.yinzhengjie.org.cn
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# nginx -t
nginx: the configuration file /yinzhengjie/softwares/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /yinzhengjie/softwares/nginx/conf/nginx.conf test is successful
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# ss -ntl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN *: *:*
LISTEN ::: :::*
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# nginx
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# ss -ntl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN *: *:*
LISTEN *: *:*
LISTEN ::: :::*
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]#
5>.访问web服务器
浏览器地址栏输入:"http://node101.yinzhengjie.org.cn/",访问结果如下图所示。
浏览器地址栏输入:"http://node101.yinzhengjie.org.cn/python/",访问结果如下图所示。
浏览器地址栏输入:"http://node101.yinzhengjie.org.cn/golang/",访问结果如下图所示。
Nginx 核心配置-新建一个web站点的更多相关文章
- nginx下配置多个web服务
参考 nginx配置详解 nginx反向代理与负载均衡详解 一.nginx简介: Nginx("engine x")是一款是由俄罗斯的程序设计师Igor Sysoev所开发高性能 ...
- Nginx 核心配置-作为下载服务器配置
Nginx 核心配置-作为下载服务器配置 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.无限速版本的下载服务器 1>.查看主配置文件 [root@node101.yinz ...
- Nginx 核心配置-自定义日志路径及清空日志注意事项
Nginx 核心配置-自定义日志路径及清空日志注意事项 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.关于日志清空注意事项 1>.nginx服务写访问日志是基于acces ...
- Nginx 核心配置-检测文件是否存在
Nginx 核心配置-检测文件是否存在 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. try_files会按顺序检查文件是否存在,返回第一个找到的文件或文件夹(结尾加斜线表示为文件 ...
- Nginx 核心配置-自定义错误页面
Nginx 核心配置-自定义错误页面 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 生产环境中错误页面一般都是UI或开发工程师提供的,他们已经在软件中定义好了,我们这里就简单写个h ...
- Nginx 核心配置-location的匹配案例实战篇
Nginx 核心配置-location的匹配案例实战篇 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.location语法规则介绍 在没有使用正则表达式的时候,nginx会先在 ...
- Nginx 核心配置-单节点实现多域名访问
Nginx 核心配置-单节点实现多域名访问 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.试验环境说明 1>.虚拟机环境说明 [root@node101.yinzheng ...
- Nginx 核心配置-可优化配置参数
Nginx 核心配置-可优化配置参数 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.nginx的官网查看指令帮助信息方法 1>.打开nginx的官网(https://ng ...
- Nginx 核心配置-作为上传服务器配置
Nginx 核心配置-作为上传服务器配置 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.关键参数说明 client_max_body_size 1m: 设置允许客户端上传单 ...
随机推荐
- 求职-如何选择offer
如何选择offer呢?下面我们从这几部分一起聊聊: HR问你目前拿到哪几个offer了怎么回答好? 选择小公司还是大公司? 为什么刚入行不要去没有人带的部门? 正式员工.合同工和外包人员有什么区别? ...
- html 单元格合并
<table border="1" style={{margin:200}}> <tbody> <tr> <th colspan=&quo ...
- 第02组 Alpha事后诸葛亮
目录 1. 组长博客(2分) 2. 总结思考(27分) 2.1. 设想和目标(2分) 2.2. 计划(5分) 2.3. 资源(3分) 2.4. 变更管理(4分) 2.5. 设计/实现(4分) 2.6. ...
- CodeForces 487E Tourists(圆方树+线段树+树链剖分)
题意 \(n\) 个点 \(m\) 条边的无向连通图,每个点有点权,\(q\) 个要求,每次更新一个点的点权或查询两点间路径权值最小的点最小的路径. 思路 算是圆方树的板子吧?圆方树处理的主要 ...
- buildroot output子目录
build/ 包含所有的源文件,包括 Buildroot 所需主机工具和选择的包,这个目录包含所有 模块源码. host/ 主机端编译需要的工具包括交叉编译工具. images/ 包含压缩好的根文件系 ...
- Eclipse:设置自动补全,提高编程效率
一.设置自动补全 1.进入eclipse的window里的perferences页面 2.找到java->Editor->Content Assist设置界面 3.在Auto activa ...
- IdentityServer4实现原理
OAuth&OpenIDConnect是什么? 最近因为工作的原因,大概有两个月时间没写博客了,本来今年给自己的目标是每个月写一篇,或许记录工作中踩过的一些坑,或许学习一些新的技术框架.说实话 ...
- 学习《Linux网络安全技术与实现》,正文为1、2章的笔记,后面的等待更新
博客地址:http://www.cnblogs.com/zengjianrong/p/3280276.html
- 【leetcode-152】 乘积最大子序列
给定一个整数数组 nums ,找出一个序列中乘积最大的连续子序列(该序列至少包含一个数). 示例 1: 输入: [2,3,-2,4]输出: 6解释: 子数组 [2,3] 有最大乘积 6.示例 2: 输 ...
- 【转】常用PLC通讯协议
三菱FX系列PLC通讯测试 发送帧(Hex): 起始(STX) 02 命令(CMD) 30 首地址(ADDRESS) 30 30 41 30 字节数(BYTES) 30 31 终止(ETX) 03 校 ...