CentOS7 nginx
vi /etc/sysconfig/network-scripts/ifcfg-ens33
systemctl restart network
测试
ping qq.com
vi /etc/sysconfig/network-scripts/ifcfg-ens33
TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=static
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=ens33
UUID=65f7be04-3c27-405a-a8b0-808e8702f4ee
DEVICE=ens33
ONBOOT=yes
IPADDR=192.168.204.135
NETMASK=255.255.255.0
GATEWAY=192.168.44.1
DNS1=8.8.8.8
systemctl restart network
/usr/share/nginx
├── html
│ ├── 404.html
│ ├── 50x.html
│ ├── en-US -> ../../doc/HTML/en-US
│ ├── icons
│ │ └── poweredby.png -> ../../../pixmaps/poweredby.png
│ ├── img -> ../../doc/HTML/img
│ ├── index.html -> ../../doc/HTML/index.html //显示 网页上,输入ip即可访问
│ ├── nginx-logo.png
│ └── poweredby.png -> nginx-logo.png
└── modules
Nginx多进程模型和基本请求流程
vim /etc/nginx/nginx.conf
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;
events {
worker_connections 1024;
}
http {
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;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 4096;
include /etc/nginx/mime.types;
default_type application/octet-stream;
include /etc/nginx/conf.d/*.conf;
server {
listen 80;
listen [::]:80;
server_name _;
root /usr/share/nginx/html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
error_page 404 /404.html;
location = /404.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
全局块:
配置影响nginx全局的指令。⼀般有运⾏nginx服务器的⽤户组,nginx进程pid存放路径,⽇志存放路径,配置⽂件引⼊,worker process值等。
events块:
配置影响nginx服务器或与⽤户的⽹络连接。有每个进程的最⼤连接数,选取哪种事件驱动模型处理连接请求,是否允许同时接收多个网络连接,开启多个网络连接序列化等。
http全局块:
http 全局块配置的指令包括文件引入、 MIME-TYPE 定义、日志自定义、连接超时时间、单连接请求数上限等。
server块:
虚拟主机是一种特殊的软硬件技术,它可以将网络上的每一台计算机分成多个虚拟主机,每个虚拟主机可以独立对外提供www服务,这样就可以实现一台主机对外提供多个web服务,每个虚拟主机之间是独立的,互不影响的,每个 server 块就相当于一个虚拟主机。
通过nginx可以实现虚拟主机的配置,nginx支持三种类型的虚拟主机配置:
基于ip的虚拟主机, (一块主机绑定多个ip地址)
基于域名的虚拟主机(servername)
基于端口的虚拟主机(listen如果不写ip端口模式)
location块:
配置请求的路由,以及各种匹配成功后的处理情况。
反向代理
在location块中添加配置
负载均衡
需要在http块中添加如下配置,默认轮询方式进行负载
动静分离
在前后端不分离的项目中,项目的static下往往存放一下静态资源(css、js等),我们通过配置让用户直接通过nginx访问这些静态资源,而不用通过项目再去访问它们,以提高项目的并发能力。
在nginx安装目录下创建static文件夹,并将项目中static下的静态资源放到/nginx/static下,然后在配置文件中添加如下配置。
sendfile on;

│ ├── vod
│ │ └── index.html
│ └── www
│ └── index.html
vim /etc/nginx/nginx.conf
nginx.conf 配置
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;
events {
worker_connections 1024;
}
http {
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;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 4096;
include /etc/nginx/mime.types;
default_type application/octet-stream;
include /etc/nginx/conf.d/*.conf;
server {
listen 80;
listen [::]:80;
server_name www.yangyongsheng.top;
root /usr/share/nginx/html/www/www;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
error_page 404 /404.html;
location = /404.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
server {
listen 80;
listen [::]:80;
server_name vod.yangyongsheng.top;
root /usr/share/nginx/html/www/vod;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
error_page 404 /404.html;
location = /404.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
}
www域名指向www下的html
server {
listen 80;
listen [::]:80;
server_name www.yangyongsheng.top;
root /usr/share/nginx/html/www/www;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
error_page 404 /404.html;
location = /404.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
访问www.yangyongsheng.top
显示
this is vod
server {
listen 80;
listen [::]:80;
server_name vod.yangyongsheng.top;
root /usr/share/nginx/html/www/vod;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
error_page 404 /404.html;
location = /404.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
访问vod.yangyongsheng.top
显示
this is vod
6.常见的Nginx正则表达式代表的含义
^ :匹配输入字符串的起始位置
$ :匹配输入字符串的结束位置
* :匹配前面的字符零次或多次。如“ol*”能匹配“o”及“ol”、“oll”
+ :匹配前面的字符一次或多次。如“ol+”能匹配“ol”及“oll”、“olll”,但不能匹配“o”
? :匹配前面的字符零次或一次,例如“do(es)?”能匹配“do”或者“does”,”?”等效于”{0,1}”
. :匹配除“\n”之外的任何单个字符,若要匹配包括“\n”在内的任意字符,请使用诸如“[.\n]”之类的模式
\ :将后面接着的字符标记为一个特殊字符或一个原义字符或一个向后引用。如“\n”匹配一个换行符,而“\$”则匹配“$”
\d :匹配纯数字
\w :匹配字母或数字或下划线或汉字
\s :匹配任意的空白符
\b :匹配单词的开始或结束
{n} :重复 n 次
{n,} :重复 n 次或更多次
{n,m} :重复 n 到 m 次
[] :定义匹配的字符范围
[c] :匹配单个字符 c
[a-z] :匹配 a-z 小写字母的任意一个
[a-zA-Z0-9] :匹配所有大小写字母或数字
() :表达式的开始和结束位置 例如:(jpg|gif|swf|)
| :或运算符
nginx负载均衡配置,主要包含两部分,一部分是对监听器(server模块)的配置,另一部分是对后端server池(upstream模块)的配置。
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;
events {
worker_connections 1024;
}
http {
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;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 4096;
include /etc/nginx/mime.types;
default_type application/octet-stream;
include /etc/nginx/conf.d/*.conf;
upstream httpds {
server 192.168.204.138:80;
server 192.168.204.136:80;
}
server {
listen 80;
server_name localhost;
# root /usr/share/nginx/html;
location / {
proxy_pass http://httpds;
root html;
index index.html index.htm;
}
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
error_page 404 /404.html;
location = /404.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
}
主要改地方
upstream httpds {
server 192.168.204.138:80;
server 192.168.204.136:80;
}
server {
location / {
proxy_pass http://httpds;
}
}
本文作者: 永生
本文链接: https://yys.zone/detail/?id=262
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明出处!
发表评论
评论列表 (0 条评论)
暂无评论,快来抢沙发吧!