Nginx 核心配置-新建一个web站点

时间:2024-04-17 16:05:12

              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/",访问结果如下图所示。

Nginx 核心配置-新建一个web站点

  浏览器地址栏输入:"http://node101.yinzhengjie.org.cn/python/",访问结果如下图所示。

Nginx 核心配置-新建一个web站点

  浏览器地址栏输入:"http://node101.yinzhengjie.org.cn/golang/",访问结果如下图所示。

Nginx 核心配置-新建一个web站点