web应用-Nginx学习笔记02-配置项结构和内容

操作环境介绍

操作系统信息

ubuntu18.04

1.如何安装得到一个nginx应用?

安装nginx应用,安装方式,通过apt方式安装;

apt install nginx

nginx版本信息查看

bash 复制代码
root@ub1804:/etc/nginx# nginx -v
nginx version: nginx/1.14.0 (Ubuntu)

2.nginx的web应用应该如何配置?

2.1配置文件有哪些?在哪个路径下,名字叫什么?哪些是基础核心的配置?

对应版本的配置文件路径/et/nginx/,配置文件/etc/nginx/nginx.conf

bash 复制代码
root@ub1804:/etc/nginx# tree
.
├── conf.d
├── fastcgi.conf
├── fastcgi_params
├── koi-utf
├── koi-win
├── mime.types
├── modules-available
├── modules-enabled
│   ├── 50-mod-http-geoip.conf -> /usr/share/nginx/modules-available/mod-http-geoip.conf
│   ├── 50-mod-http-image-filter.conf -> /usr/share/nginx/modules-available/mod-http-image-filter.conf
│   ├── 50-mod-http-xslt-filter.conf -> /usr/share/nginx/modules-available/mod-http-xslt-filter.conf
│   ├── 50-mod-mail.conf -> /usr/share/nginx/modules-available/mod-mail.conf
│   └── 50-mod-stream.conf -> /usr/share/nginx/modules-available/mod-stream.conf
├── nginx.conf
├── proxy_params
├── scgi_params
├── sites-available
│   └── default
├── sites-enabled
│   └── default -> /etc/nginx/sites-available/default
├── snippets
│   ├── fastcgi-php.conf
│   └── snakeoil.conf
├── uwsgi_params
└── win-utf

6 directories, 19 files
root@ub1804:/etc/nginx# pwd
/etc/nginx

2.2配置文件里面有什么内容,如何解读配置文件?

通过文本编辑工具vim查看nginx.conf配置文件内容,得到配置文件里面有什么内容的答案;

通过参考资料的阅读,可以将配置文件内容的结构进行分类;

nginx.conf配置文件内容如下:

bash 复制代码
  1 user www-data;
  2 worker_processes auto;
  3 pid /run/nginx.pid;
  4 include /etc/nginx/modules-enabled/*.conf;
  5 
  6 events {
  7         worker_connections 768;
  8         # multi_accept on;
  9 }
 10 
 11 http {
 12 
 13         ##
 14         # Basic Settings
 15         ##
 16 
 17         sendfile on;
 18         tcp_nopush on;
 19         tcp_nodelay on;
 20         keepalive_timeout 65;
 21         types_hash_max_size 2048;
 22         # server_tokens off;
 23 
 24         # server_names_hash_bucket_size 64;
 25         # server_name_in_redirect off;
 26 
 27         include /etc/nginx/mime.types;
 28         default_type application/octet-stream;
 29 
 30         ##
 31         # SSL Settings
 32         ##
 33 
 34         ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
 35         ssl_prefer_server_ciphers on;
 36 
 37         ##
 38         # Logging Settings
 39         ##
 40 
 41         access_log /var/log/nginx/access.log;
 42         error_log /var/log/nginx/error.log;
 43 
 44         ##
 45         # Gzip Settings
 46         ##
 47 
 48         gzip on;
 49 
 50         # gzip_vary on;
 51         # gzip_proxied any;
 52         # gzip_comp_level 6;
 53         # gzip_buffers 16 8k;
 54         # gzip_http_version 1.1;
 55         # gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
 56 
 57         ##
 58         # Virtual Host Configs
 59         ##
 60 
 61         include /etc/nginx/conf.d/*.conf;
 62         include /etc/nginx/sites-enabled/*;
 63 }
 64 

如何解读配置文件呢,可以从nginx架构模型和http协议方面入手

nginx架构模型通过master和worker的方式,实现单线程高效工作;显示对应的nginx的进程id,对应worker进程的个数,通过nginx对应的扩展模块,实现一些定制增强功能,以上是配置文件第1到9行的解读,接着向下不难看出这是一个http块的配置,所有http相关的配置都在()括号范围之中,然后是一些http协议连接的细节,是否开启长连接,是否启用ssl加密,是否启用数据压缩等的配置。接着往下面看,可以看到关于日志这块,nginx将连接和错误的日志记录在对应路径下,并且有清晰好理解的名字,access.log/error.log,在然后是虚拟主机配置,最后是注释部分关于mail块的配置,暂时按下不表。

2.3通过配置文件的解读,看看相关的路径内容(从上往下的顺序)

1.include /etc/nginx/modules-enabled/*.conf;

这个是nginx模块的汇总仓库,可以看到原始的配置文件在其他地方;

bash 复制代码
root@ub1804:/etc/nginx/modules-enabled# ls -l
总用量 8
lrwxrwxrwx 1 root root 54 6月  20 22:04 50-mod-http-geoip.conf -> /usr/share/nginx/modules-available/mod-http-geoip.conf
lrwxrwxrwx 1 root root 61 6月  20 22:04 50-mod-http-image-filter.conf -> /usr/share/nginx/modules-available/mod-http-image-filter.conf
lrwxrwxrwx 1 root root 60 6月  20 22:04 50-mod-http-xslt-filter.conf -> /usr/share/nginx/modules-available/mod-http-xslt-filter.conf
lrwxrwxrwx 1 root root 48 6月  20 22:04 50-mod-mail.conf -> /usr/share/nginx/modules-available/mod-mail.conf
lrwxrwxrwx 1 root root 50 6月  20 22:04 50-mod-stream.conf -> /usr/share/nginx/modules-available/mod-stream.conf
2.include /etc/nginx/mime.types;

http块配置中,关于数据文件的类型定义,这个MME的数据类型,一开始是为了邮件中发送非文本的数据内容,并且能够根据数据类型提示调用对应解封装模块,
数据文件的类型分类:文本类型,图像类型,应用数据,音频类型,视频类型

bash 复制代码
root@ub1804:/etc/nginx# cat mime.types 

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/tiff                            tif tiff;
    image/vnd.wap.wbmp                    wbmp;
    image/x-icon                          ico;
    image/x-jng                           jng;
    image/x-ms-bmp                        bmp;
    image/svg+xml                         svg svgz;
    image/webp                            webp;

    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.ms-excel              xls;
    application/vnd.ms-fontobject         eot;
    application/vnd.ms-powerpoint         ppt;
    application/vnd.wap.wmlc              wmlc;
    application/vnd.google-earth.kml+xml  kml;
    application/vnd.google-earth.kmz      kmz;
    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;

    application/vnd.openxmlformats-officedocument.wordprocessingml.document    docx;
    application/vnd.openxmlformats-officedocument.spreadsheetml.sheet          xlsx;
    application/vnd.openxmlformats-officedocument.presentationml.presentation  pptx;

    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;
}
3.include /etc/nginx/sites-enabled/*

可用站点的默认配置

通过之前目录结构的观察,可以看到默认页面index的路径配置在/etc/nginx/sites-available/default中

bash 复制代码
── sites-available
│   └── default
├── sites-enabled
│   └── default -> /etc/nginx/sites-available/default

然后接着追查 default文件内容,可以看到默认的server块配置的内容,得到关于http页面配置端口静态页面文件路径/var/www/html等关键信息。

default配置文件中的默认首页index 对应配置为三个,选哪一个呢?这几个文件在哪个路径下呢?

bash 复制代码
index index.html index.htm index.nginx-debian.html;

在/usr/share/nginx中找到idex.html;

在/var/www/html/中找到index.nginx-debian.html;这两个的默认内容一模一样;

default文件内容如下:

bash 复制代码
##
# You should look at the following URL's in order to grasp a solid understanding
# of Nginx configuration files in order to fully unleash the power of Nginx.
# https://www.nginx.com/resources/wiki/start/
# https://www.nginx.com/resources/wiki/start/topics/tutorials/config_pitfalls/
# https://wiki.debian.org/Nginx/DirectoryStructure
#
# In most cases, administrators will remove this file from sites-enabled/ and
# leave it as reference inside of sites-available where it will continue to be
# updated by the nginx packaging team.
#
# This file will automatically load configuration files provided by other
# applications, such as Drupal or Wordpress. These applications will be made
# available underneath a path with that package name, such as /drupal8.
#
# Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.
##

# Default server configuration
#
server {
	listen 80 default_server;
	listen [::]:80 default_server;

	# SSL configuration
	#
	# listen 443 ssl default_server;
	# listen [::]:443 ssl default_server;
	#
	# Note: You should disable gzip for SSL traffic.
	# See: https://bugs.debian.org/773332
	#
	# Read up on ssl_ciphers to ensure a secure configuration.
	# See: https://bugs.debian.org/765782
	#
	# Self signed certs generated by the ssl-cert package
	# Don't use them in a production server!
	#
	# include snippets/snakeoil.conf;

	root /var/www/html;

	# Add index.php to the list if you are using PHP
	index index.html index.htm index.nginx-debian.html;

	server_name _;

	location / {
		# First attempt to serve request as file, then
		# as directory, then fall back to displaying a 404.
		try_files $uri $uri/ =404;
	}

	# pass PHP scripts to FastCGI server
	#
	#location ~ \.php$ {
	#	include snippets/fastcgi-php.conf;
	#
	#	# With php-fpm (or other unix sockets):
	#	fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
	#	# With php-cgi (or other tcp sockets):
	#	fastcgi_pass 127.0.0.1:9000;
	#}

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


# Virtual Host configuration for example.com
#
# You can move that to a different file under sites-available/ and symlink that
# to sites-enabled/ to enable it.
#
#server {
#	listen 80;
#	listen [::]:80;
#
#	server_name example.com;
#
#	root /var/www/example.com;
#	index index.html;
#
#	location / {
#		try_files $uri $uri/ =404;
#	}
#}

/etc/nginx中的其他配置项内容的阅读

proxy_params配置文件

bash 复制代码
root@ub1804:/etc/nginx# cat proxy_params 
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;

uwsgi_params配置文件

bash 复制代码
root@ub1804:/etc/nginx# cat uwsgi_params 

uwsgi_param  QUERY_STRING       $query_string;
uwsgi_param  REQUEST_METHOD     $request_method;
uwsgi_param  CONTENT_TYPE       $content_type;
uwsgi_param  CONTENT_LENGTH     $content_length;

uwsgi_param  REQUEST_URI        $request_uri;
uwsgi_param  PATH_INFO          $document_uri;
uwsgi_param  DOCUMENT_ROOT      $document_root;
uwsgi_param  SERVER_PROTOCOL    $server_protocol;
uwsgi_param  REQUEST_SCHEME     $scheme;
uwsgi_param  HTTPS              $https if_not_empty;

uwsgi_param  REMOTE_ADDR        $remote_addr;
uwsgi_param  REMOTE_PORT        $remote_port;
uwsgi_param  SERVER_PORT        $server_port;
uwsgi_param  SERVER_NAME        $server_name;

snippets文件目录下snakeoil.conf文件和fastcgi-php.conf文件内容

snippets 单词翻译:片段

bash 复制代码
root@ub1804:/etc/nginx/snippets# ls
fastcgi-php.conf  snakeoil.conf
root@ub1804:/etc/nginx/snippets# tree
.
├── fastcgi-php.conf
└── snakeoil.conf

0 directories, 2 files
bash 复制代码
root@ub1804:/etc/nginx/snippets# cat snakeoil.conf 
# Self signed certificates generated by the ssl-cert package
# Don't use them in a production server!

ssl_certificate /etc/ssl/certs/ssl-cert-snakeoil.pem;
ssl_certificate_key /etc/ssl/private/ssl-cert-snakeoil.key;
bash 复制代码
root@ub1804:/etc/nginx/snippets# cat fastcgi-php.conf 
# regex to split $uri to $fastcgi_script_name and $fastcgi_path
fastcgi_split_path_info ^(.+\.php)(/.+)$;

# Check that the PHP script exists before passing it
try_files $fastcgi_script_name =404;

# Bypass the fact that try_files resets $fastcgi_path_info
# see: http://trac.nginx.org/nginx/ticket/321
set $path_info $fastcgi_path_info;
fastcgi_param PATH_INFO $path_info;

fastcgi_index index.php;
include fastcgi.conf;

小结:

关于nginx的安装,一条命令就完成了,然后就是关于nginx配置项的理解,文件在哪里,如何解读配置文件,其他配置文件的作用是啥,体现在页面中的哪些位置上呢?

##END提示,就先到这里吧,>!<

相关推荐
丰锋ff16 分钟前
考研英一学习笔记
笔记·学习·考研
小墨宝17 分钟前
js 生成pdf 并上传文件
前端·javascript·pdf
hnlucky32 分钟前
redis 数据类型新手练习系列——Hash类型
数据库·redis·学习·哈希算法
HED32 分钟前
用扣子快速手撸人生中第一个AI智能应用!
前端·人工智能
DN金猿36 分钟前
使用npm install或cnpm install报错解决
前端·npm·node.js
丘山子37 分钟前
一些鲜为人知的 IP 地址怪异写法
前端·后端·tcp/ip
Invinciblenuonuo40 分钟前
FreeRTOS学习笔记【10】-----任务上下文切换
笔记·学习
好奇龙猫42 分钟前
日语学习-日语知识点小记-构建基础-JLPT-N4阶段(11): てあります。
学习
志存高远661 小时前
Kotlin 的 suspend 关键字
前端
www_pp_1 小时前
# 构建词汇表:自然语言处理中的关键步骤
前端·javascript·自然语言处理·easyui