nginx 企业应用配置-1 | 学习笔记(二)

简介: 快速学习nginx 企业应用配置-1。

开发者学堂课程【Linux Web服务器Nginx搭建与配置nginx 企业应用配置-1学习笔记,与课程紧密联系,让用户快速学习知识.

课程地址:https://developer.aliyun.com/learning/course/579/detail/7988


nginx 企业应用配置-1


目录

一.nginx配置

二.ngx_http_core_module


二、ngx_http_core_module

  • ngx_http_core_module(核心模块)

在官方文件中有很多指令,如listen,server,server-name,root,只写都是经常使用的。

  • 与套接字相关的配置
  • 1、server (...)
  • 配置一个虚拟主机

参考:

server {

listen address[:PORT]|PORT;

server_name SERVER_NAME;

root /PATH/TO/DOCUMENT_ROOT;

举例:当我们搭建一个网站时以 server这一个语句块来组成的,工作在http语句块中,server-name为网站名,root为存放主页路径。

http {

log_format main  .Sremote-addr-Sremote-user[Stime-local]”Srequest”

.Sstatus Sbody-bytes-sent”Shttp-referer”

‘”Shttp-user-agent””Shttp-x-forwarded-for”’

access_log /var/log/ngin/access.log main;

sendfile          on

tcp nopush       on;

tcp nodelay        on;

keepalive timeout   65·

types hash max size 2048;

include             /etc/nginx/mime.types;

defauit_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          80default server;

listen          [::]:80 default_server;

server-name       -;

root            /usr/share/nginx/html;

# Load configuration files for the default server block.

include/etc/nginx/default.d/*.conf;

location / {

}

error_page 404 /404.html;

location =/40x.html

我们可以自己建立一个目录或在*.conf搭建一个网站

[root@centos7 nginx]#

[root@centos7 nginx]#cd/ect/nginx/conf.d/

[root@centos7 nginx]#ls

[root@centos7 nginx]#mkdir vhosts

[root@centos7 nginx]#cd vhosts/

[root@centos7 nginx]#vim a.com.conf

Server {

Listen 80;

Server-name www.a.com

Root/data/sitea/;

}

Server{

Listen 80

Server-name www.b.com www.c.com

}

要向方便管理,只需要新建一个文件存放即可,就算出现错误,只用在这个文件夹中进行查找,不影响其他网站工作。

官方listen说明

Syntax: listen address[:port][default server][ssl] [http2 | spdy

[proxy protocol]

[setfit=number][fastopen=number][backlog=number][rcvbu

=size][-sndbuf=size]

[accept-filter=filter][keepidle][bind] [ipv6only on|off]Lreusep

ort]

[so-keepalive=on|off|keepdie]:[keepintvl]:[keepcent]

lisent port [default-server][ssl][http2|spdy][proxy-protocol][setfib=number]

[fastopen=number][backlog=number][ rcvbut=size][sndbuf=size][accept-filter=filter]

[keepintvl] :[keepcent]

[so-keepalive=on|off|keepdie]:[keepintvl]:[keepcent]

listenunix:port[default-server][ssl] [http2 |spdy] [proxy_protocol]

[backlog=number][rcvbuf=size][sedbuf=size][accept filter=filter][deferred][bind]

[so-keepalive=on|off|keepidle]:[ Keepintvl][ keeocntl]

Default: listen *:80 *:8000

Context:server

举例:

Sets the address and port for IP, or the path for a UNIX-domain socket on which the serve-r will accept requests. Both address and port, or only address or only port can be specifie-d. An address may also be a hostname, for example:

listen 127.0.0.1:8000;

listen 127.0.0.1;

listen 8000

listen localhos 8000

2、listen PORT|address[:port]|unix:/PATH/TO/SOCKET_FILE

listen address[:port][default_server] [ssI] [http2 | spdy] [backlog=number] [rcvbuf=size][sndbuf=size]

default_server 设定为默认虚拟主机

ssl                  限制仅能够通过Ssl连接提供服务

backlog=number     超过并发连接数后,新请求进入后援队列的长度

rcvbuf=size        接收缓冲区大小

sndbuf=size       发送缓冲区大小

注意:

(1)基于port;

  listen PORT         指令监听在不同的端口

(2) 基于ip的虚拟主机

  listen IP:PORT        IP 地址不同

(3) 基于hostname

server_name fodn;       指令指向不同的主机名

3. server_name name..;

  • 虚拟主机的主机名称后可跟多个由空白字符分隔的字符串
  • 支持*通配任意长度的任意字符
    server_name.magedu.com www.magedu.*
  • 支持~起始的字符做正则表达式模式匹配,性能原因慎用server name   ~^www\d+\.magedu\.com$(\d表示数字;+表示一个或一个以上,至少一个;
    说明:\d 表示 [0-9]
  • 匹配优先级机制从高到低
  1. 首先是字符串精确匹配 如:www.magedu.com
    (2)左侧*通配符 如:*.magedu.com
    (3)右侧*通配符 如:www.magedu.*
    (4) 正则表达式如:~^*\.magedu\.com$
    (5) default_server

4、tcp_nodelay on | off;

在keepalived模式下的连接是否启用TCP_NODELAY选项当为off时,延迟发送,合并多个请求后再发送。

默认On时,不延迟发送

注:启用keepalived模式意味着用户发请求后不立即断开,可继续发多个请求过来,off为合并多个请求一起回应,on为不延迟发送,来一个就立即回应。延迟发送对服务器资源有一定的好处,西南节约,但一般由于考虑用户感受所以默认为on。

可用于:http, server, location

5、sendfile on | off;

是否启用sendfile功能,在内核中封装报文直接发送

默认Off

6 、server_tokens on | off | build | string

是否在响应报文的Server首部显示nginx版本

server_tokens在各网站自己的语句块中实现隐藏

例:http {

log_format main ‘Sramotu -addr-Sremote-user[Scimo local] “Sroouast"

              ‘Sstatus Sbody bytew-sent”SHTTP_REFERER”

‘”Shttp_user-agent””Shttp_x_forwarded_for” access _loa /var/log/nginx/access.log main;

server_tokens  off;

sendfile            on;

tcp_nopush         on

tcp_nodelay         on

keepalive_timeout    65;

types_hash_max_size 2048;

include              /etc/nginx/mime.types;

default_type          application/octet-stream;

#Load modular confiaurationfiles from the /etc/nginx/conf.d directory.  

#Seehttp://nginx.org/en/docs/ngx_core_module.html#include

#for more information.

include /etc/nginx/conf.d/*.conf;

Include /etc/nginx/conf.d/vhosts/*.conf;

server

listen 80

#listen 80default_server;

listen [::]:80default._server;

Se VeI".name

ginx/nginx.conf 91L,2573C written

whosts4whosts]/nginx

显得不够安全,所以进行隐藏处理。

隐藏后:

[root@centos7 ~]#cur1 -I bbs.b.com

cur1:(6) Could not resolvehost :bbs.b.com;Unknown error

[root@centos7 -]#curl -工 www.b.com

HTTP/1.1 200 OK

Server:nginx/1.12.2

Date: wed,04 JuT201806:54:34 GMT

Content-Type:text/htm1

Content-Length: 10

Last-Modified:Wed 04Ju1 2018 06:37:22 GMT

Connection: keep-alive

ETag:"5b3c6b22-a

Accept-Ranges:bytes

定义路径相关的配置

7.root

设置web资源的路径映射;用于指明请求的URL所对应的文档的目录路径,可用于http, server, location, if in location

server {

root /data/www/vhost1

}

示例

http://www.magedu.com/images/logo.jpg

 --> /data/www/vhosts/images/logo.jpg

  1. location[=|~|~*|^~]uri{...}

location @name {…}(可以定义一个位置。针对某一个位置进行额外处理,放在server语句块中。

功能:针对某一个具体的路径来定义相应的规则。

Server  {

Listen 80

Server-name*.a.com

Location / {        /代表额外设置

Root /data/sitea/

}

“a.com.conf"8L.105C written

[root@centos7 vhosts]#cp/data/sitea/news/ c^c

[root@centos7 vhosts]#mkdir/app

Mkdir:cannot create directory/app:f;ie exists

[root@centos7 vhosts]#vim a.com.conf^c

[root@centos7 vhosts]#cp -r data/sitea/naws /app

[root@centos7 vhosts]#tree app/

/app/

  news

index.html

1 directory.1 file

[root@centos7 vhosts]#mkdir /app/sitea

[root@centos7 vhosts]# cd/app/sitea

[root@centos7 siteal]#ls

[root@centos7 sitea]#cd..

[root@centos7 app]#ls

News sitea

[root@centos7 app]#mv news/ sitea/

[root@centos7 app]# tree

sitea

  news

index.html

2directories.1 file

r[root@centos7 app]#tree /app

/app

sitea

news

index.html

2 directorise.1 file

[root@centos7 app]#vim sitea/news

在一个server中location配置段可存在多个,用于实现从uri到文件系统的路径映射;ngnix会根据用户请求的URI来检查定义的所有location,并找出一个最佳匹配,而后应用其配置

示例:

server {..

  server-name www.magedu.com;

  location /images/ {

      root /data/imgs/

}

}

http://www.magedu.com/images/logo.jpg

-->/data/imgs/images/logo.jpg

示例结束

=:对URI做精确匹配;

location=/ {

}

http://www.magedu.com/ 匹配

http://www.magedu.com/index.html 不匹配

^~:     对URI的最左边部分做匹配检查,不区分字符大小写

~:       对URI做正则表达式模式匹配,区分字符大小写

~*       对URI做正则表达式模式匹配,不区分字符大小写

不带符号:匹配起始于此uri的所有的uri

匹配优先级从高到低: =,^~,~/~*,不带符号

示例:

root/vhosts/www/htdocs/

  http://www.magedu.com/index.html

   --> /vhosts/www/htdocs/index.html

server {

 root /vhosts/www/htdocs/

location /admin/ {

root /webapps/app1/data/

}

}

http://www.magedu.com/admin/index.html

--> /webapps/app1/data/admin/index.html

location示例

location =/{            http://www.magedu.com/匹配语句块A

[ configuration A ]

}                                 http://www.magedu.com/

location /{                       index.html

[ configuration B ]

}                                 http://www.magedu.com/

location /documents/{              documents/logo.jpg

匹配C E但由于C不带符号优先级低于E所以E生效

[ configuration C ]

}                                 http://www.magedu.com/

location^~ /images/{               documents/linux.txt

 [ configuration D ]

)                                 Ihttp://www.magedu.com

location ~* \(gifljpgljpeg)${    /images/logo.jpeg

 [ configuration E]

)

9、alias path;

路径别名,文档映射的另一种机制;仅能用于location上下文

示例

http://www.magedu.com/bbs/index.php

location /bbs/{

alias /web/forum/;

}   --> /web/forum/index.html

location/bbs/{

root /web/forum/;

}   -->/web/forum/bbs/index.html

注意:location中使用root指令和alias指令的意义不同

(a) root,给定的路径对应于location中的/uri/左侧的/

(b) alias,给定的路径对应于location中的/uri/右侧的/

10、index file....

指定默认网页文件,注意:ngx_http_index_module模块

相关文章
|
16天前
|
移动开发 前端开发 JavaScript
前端vue2、vue3去掉url路由“ # ”号——nginx配置(一)
前端vue2、vue3去掉url路由“ # ”号——nginx配置
49 0
|
16天前
|
JavaScript 前端开发 应用服务中间件
angular引入包、路由权限配置、打包问题与nginx配置问题(简单部署)
angular引入包、路由权限配置、打包问题与nginx配置问题(简单部署)
24 0
|
16天前
|
前端开发 JavaScript 应用服务中间件
前端vue2、vue3去掉url路由“ # ”号——nginx配置(二)
前端vue2、vue3去掉url路由“ # ”号——nginx配置
47 0
|
1天前
|
应用服务中间件 nginx
nginx配置集群轮训策略
nginx配置集群轮训策略
9 0
|
2天前
|
安全 网络协议 应用服务中间件
一文读懂HTTPS⭐揭秘加密传输背后的原理与Nginx配置攻略
一文读懂HTTPS⭐揭秘加密传输背后的原理与Nginx配置攻略
|
9天前
|
应用服务中间件 PHP nginx
php如何实现检测nginx配置的正确性
请确保在执行此操作时,PHP有足够的权限来执行Nginx命令和访问Nginx配置文件。另外,将上述代码嵌入到您的应用程序中时,要注意安全性,以防止潜在的命令注入攻击。
50 3
|
16天前
|
安全 应用服务中间件 网络安全
linux_nginx中添加ssl配置(open ssl)
linux_nginx中添加ssl配置(open ssl)
25 1
|
16天前
|
JSON JavaScript 前端开发
vue2_vite.config.js的proxy跨域配置和nginx配置代理有啥区别?
vue2_vite.config.js的proxy跨域配置和nginx配置代理有啥区别?
32 1
|
19天前
|
安全 应用服务中间件 网络安全
SSL原理、生成SSL密钥对、Nginx配置SSL
现在,你的Nginx虚拟主机应该已经配置了SSL,可以通过HTTPS安全访问。确保在生产环境中使用有效的SSL证书来保护通信的安全性。
30 0
|
22天前
|
域名解析 缓存 负载均衡
Nginx正向代理域名的配置
Nginx正向代理域名的配置