时间:2023-07-14 13:00:01 | 来源:网站运营
时间:2023-07-14 13:00:01 来源:网站运营
干货篇 | Nginx 我还真不信你学不会(中):在《干货篇 | Nginx 我还真不信你学不会(上)》中,我们大概对Nginx有一了一个大概印象:我们知道了Nginx是干什么用的、什么是反向代理和正向代理以及什么是负载均衡。yum install -y nginx
systemctl start nginx
systemctl enable nginx
#关闭防火墙systemctl stop firewalld.service#暂时关闭SELinuxsetenforce 0
vim /etc/selinux/config
nginx
访问本机地址:80
/etc/nginx/nginx.conf
/etc/nginx/conf.d/
这个是一个目录,里面通常是存放虚拟主机的一些配置,比如说我们要做一个博客网站和论坛网站,就可以分别定义这两个网站的配置文件,然后存放到这个目录下。就不用都把配置写进主配置文件里,显得更简洁美观cat /etc/nginx/nginx.conf=====================第一个部分,配置文件的主区域======================user nginx; # 定义worker进程的管理用户worker_processes auto; # 定义worker进程数,auto会自动调整为cpu核数worker_cpu_affinity auto [cpumask] #提高缓存命中率,将worker与cpu绑定 CPU MASK:00000001:第一个CPU 00000010:第二个CPU worker_cpu_affinity 0001 0010 0100 1000;worker_priority number#指定worker进程的nice值,设定worker进程优先级: [-20,20](最大为19)worker_rlimit_ nofile number #worker进程所能够打开的文件数量上限,如65535error_log /var/log/nginx/error.log; # 定义错误日志pid /run/nginx.pid; # 指定存储nginx主进程的PID文件路径include /usr/share/nginx/modules/*.conf; #指明包含进来的其他配置文件片段daemon on|off #是否以守护进程方式运行nginxmaster_ process on|off #是否以master/worker模型运行nginx,默认为on
...events { worker_connections 1024; # 定义一个worker进程可以同时接受1024个请求 #总最大并发数: worker_processes * worker_ connections use method #指明并发连接请求的处理方法,默认为epoll use poll; accept_mutex on|off #处理新的连接请求的方法; on指由各个worker轮流处理新请求, Off指每个新请求的到达都会通知(唤醒)所有的worker进程,但只有一个进程可获得连接,造成 “惊群”,影响性能}
...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; # 允许sendfile方式传输文件tcp_nopush on; # 在sendfile启动下,使用TCP_CORK套接字tcp_nodelay on; # 接连接保持活动状态keepalive_timeout 65; # 超时时间types_hash_max_size 2048;# 连接超时时间include /etc/nginx/mime.types; # 文件扩展名与文件类型映射表default_type application/octet-stream; # 默认文件类型,默认为text/plaininclude /etc/nginx/conf.d/*.conf; #虚拟主机目录server { listen 80 default_server; # 指定监听的端口 listen [::]:80 default_server; server_name _; # 指定网站主机名 root /usr/share/nginx/html; # 定义站点目录的位置 include /etc/nginx/default.d/*.conf; # 定义首页文件 location / {} error_page 404 /404.html; # 定义优雅显示页面信息 location = /40x.html { } error_page 500 502 503 504 /50x.html; location = /50x.html { }}
server{ listen adress[:PORT]PORT; server_name SERVER_NAME; root /PATH/TO/DOCUMENT_ROOT;}
server{ listen 80; server_name www.a.com; root /data/web1; location / { }}
listen 80 default_ server;
listen 80listen 8080listen 8888
listen 192.168.1.1:80listen 192.168.1.2:80
server_name www.xxoo.com;
server_name www.xxoo.com www.ooxx.com;
(1) 首先是字符串精确匹配如: www.xxoo.com(2) 左侧*通配符如: *.xxoo.com(3) 右侧通配符如: www.xxoo.(4) 正则表达式如: ~^.*/.xxoo.com$(5) 默认主机 default_ server
server{ server_name www.xxoo.com; root /www/html;}http://www.xxoo.com/index.html ----> /www/html/index.html
server{ server_name www.xxoo.com; root /www/html; location / { /www/html/; }}http://www.xxoo.com/1.html ----> /www/html/1.html
server{ server_name www.xxoo.com; root /www/html; location /images{ /data/images }}http://www.xxoo.com/images ----> /data/images/1.jpg
server{ listen 80; server_name www.salted.com; location /bbs/{ alias /web/forum/; } http: //www.salted.com/bbs/index.html ----> /web/forum/index.html
路径别名:server{ listen 80; server_name www.salted.com; location /bbs/{ alias /web/forum/; http://www.salted.com/bbs/index.html ----> /web/forum/index.html路径映射server{ listen 80; server_name www.salted.com; location /bbs/{ root /web/forum/; } http://www.salted.com/bbs/index.html ----> /web/forum/bbs/index.html
server{ listen 80; server_name www.salted.com; error_page 404 /404.html; location /404.html { root /data/error/; }}
这样我们访问后如果碰到404错误的话就会显示出我们指定的错误页面。server{ listen 80; server_name www.salted.com; error_page 404=200 /404.htmL; location /404.html { root /data/error/; }}
vim /etc/nginx/conf.d/www.confserver { listen 8080; server_name www.xxoo.com; location / { root /usr/share/nginx/html; index my_index.html; }}
nginx -s reload
vim /usr/share/nginx/html/my_index.html
vim /etc/hosts192.168.244.128 www.xxoo.com
如果想要在Windows上通过访问域名就能访问到网站的话,我们要在Windows上设置域名解析server{ listen 8080 ; server_name www.xxoo.com; root /usr/share/nginx/html; location / { index my_index.html; } location /data { root /web/; } location /web/images { root /web/; }}
nginx -s reload
mkdir -pv /web/{data,images}echo "this is /web/data" > /web/data/index.htmlecho "this is /web/images" > /web/images/index.html```
server{ listen 8080 ; server_name www.xxoo.com; root /usr/share/nginx/html; error_page 404 /404.html; location / { index my_index.html; } location /data { root /web/; } location /images { root /web/; } location /404.html { root /web/pages/; }}```
创建存放错误页面的目录mkdir /web/pages
vim /web/pages/404.html<h1>this is 404 error!</h1>```
验证如下touch /etc/nginx/conf.d/{bbs,blog,www}.conf
#第一个虚拟主机vim /etc/nginx/conf.d/bbs.confserver { listen 8080; server_name bbs.com; location / { root /html/bbs; index index.html; }}#第二个虚拟主机vim /etc/nginx/conf.d/blog.confserver { listen 8080; server_name blog.com; location / { root /html/blog; index index.html; }}#第三个虚拟主机vim /etc/nginx/conf.d/www.confserver { listen 8080; server_name www.com; location / { root /html/www; index index.html; }}
mkdir -p /html/{www,bbs,blog}for name in {www,bbs,blog};do echo "<h1> $name </h1>" >/html/$name/index.html;done;;
vim /etc/hosts192.168.33.133 www.eagle.com bbs.eagle.com blog.eagle.com
nginx -s reload
curl www.com:8080<h1> www </h1>curl bbs.com:8080<h1> bbs </h1>curl blog.com:8080<h1> blog </h1>
关键词:干货