Nginx 基于域名的虚拟主机实践
1、Nginx master和worker进程
1.master进程:老大不干活,管理监控小弟 2.多个worker_processes 负责干活的,接收请求并处理。占用内存资源,数量和CPU核数相当。
2、Nginx目录结构
[root@web01 ~]$rpm -ql nginx /etc/logrotate.d/nginx #日志切割配置,/etc/logrotate.conf /etc/nginx /etc/nginx/conf.d /etc/nginx/conf.d/default.conf /etc/nginx/fastcgi_params #配置php /etc/nginx/mime.types /etc/nginx/modules /etc/nginx/nginx.conf #nginx主配置文件 /etc/nginx/scgi_params /etc/nginx/uwsgi_params #配合python /usr/lib/systemd/system/nginx-debug.service /usr/lib/systemd/system/nginx.service /usr/lib64/nginx /usr/lib64/nginx/modules /usr/libexec/initscripts/legacy-actions/nginx /usr/libexec/initscripts/legacy-actions/nginx/check-reload /usr/libexec/initscripts/legacy-actions/nginx/upgrade /usr/sbin/nginx /usr/sbin/nginx-debug /usr/share/doc/nginx-1.22.1 /usr/share/doc/nginx-1.22.1/COPYRIGHT /usr/share/man/man8/nginx.8.gz /usr/share/nginx /usr/share/nginx/html /usr/share/nginx/html/50x.html /usr/share/nginx/html/index.html /var/cache/nginx /var/log/nginx
Nginx的Web服务403错误 1.没有首页文件,index.html,index.php(没有开目录浏览功能) 2.没有站点目录以及首页文件权限
[root@web01 html]$cat /etc/logrotate.d/nginx /var/log/nginx/*.log { daily dateext #加个时间后缀 missingok rotate 52 compress delaycompress notifempty create 640 nginx adm sharedscripts postrotate if [ -f /var/run/nginx.pid ]; then kill -USR1 `cat /var/run/nginx.pid` fi endscript }
[root@web01 html]$/usr/sbin/logrotate -f /etc/logrotate.d/nginx [root@web01 html]$ll /var/log/nginx/ total 8 -rw-r----- 1 nginx adm 0 Dec 3 16:05 access.log -rw-r----- 1 nginx adm 1994 Dec 3 11:10 access.log.1 -rw-r----- 1 nginx adm 0 Dec 3 16:05 error.log -rw-r----- 1 nginx adm 2675 Dec 3 16:05 error.log.1
3、Nginx软件模块
模块名称 | 解释说明 |
---|---|
ngx_http_core_module | 包括一些核心的http参数配置,对应Nginx的配置为http区块部分 |
ngx_http_access_module | 访问控制模块,用来控制网站用户对Nginx的访问 |
ngx_http_gzip_module | 压缩模块,对Nginx返回的数据压缩,属于性能优化模块 |
ngx_http_fastcgi_module | fastcgi模块,和动态应用相关的模块,例如PHP |
ngx_http_proxy_module | proxy代理模块 |
ngx_http_upstream_module | 负载均衡模块,可以实现网站的负载均衡功能及节点的健康检查 |
ngx_http_rewrite_module | URL地址重写模块 |
ngx_http_limit_conn_module | 限制用户并发连接数及请求书模块 |
ngx_http_limit_req_module | 根据定义的key限制Nginx请求过程的速率 |
ngx_http_log_module | 访问日志模块,以指定的格式记录Nginx客户访问日志等信息 |
ngx_http_auth_basic_module | Web认证模块,设置Web用户通过账号密码访问Nginx |
ngx_http_ssl_module | ssl模块,用户加密的http连接,如https |
ngx_http_stub_status_module | 记录Nginx基本访问状态信息等模块 |
nginx的第三方模块:https://www.nginx.com/resources/wiki/modules/index.html
1 nginx如何高效访问Mysql数据库呢? 使用ngx_drizzle模块(国人编写)
2 要实现越来越多的nginx模块,发现c语言开发效率太低了,是否有其他脚本引擎呢? 使用lua脚本,ngx_lua模块(taobao两位大神的杰作)
4、Nginx配置文件
[root@web01 html]$cat /etc/nginx/nginx.conf #Nginx核心模块 Core functionality user nginx; #进程使用用户 worker_processes 1; #worker进程数量,和CPU核数相当 error_log /var/log/nginx/error.log notice; #错误日志级别,最低warn级别 # debug, info, notice, warn, error, crit, alert, or emerg pid /var/run/nginx.pid; #进程运行后的进程号所在文件。用来管理进程。 #事件模块 events { worker_connections 1024; #单个worker可以接收的并发连接数 #Nginx并发连接总量:worker_connections * worker_processes #use epoll; #官方说明可以不用配置,默认是最优配置 } #http核心模块ngx_http_core_module http { include /etc/nginx/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 /var/log/nginx/access.log main; #访问日志 sendfile on; #开启高效的传输模式 #tcp_nopush on; keepalive_timeout 65; #长连接超时时间 #gzip on; #压缩功能。 include /etc/nginx/conf.d/*.conf; #包含的配置文件目录 }
5、默认虚拟主机配置文件,简单理解就是一个网站站点
[root@web01 html]$cat /etc/nginx/conf.d/default.conf server { listen 80; server_name localhost; #access_log /var/log/nginx/host.access.log main; location / { root /usr/share/nginx/html; index index.html index.htm; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } # proxy the PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ \.php$ { # proxy_pass http://127.0.0.1; #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # #location ~ \.php$ { # root html; # fastcgi_pass 127.0.0.1:9000; # 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 { # deny all; #} }
作业:了解SOA和微服务架构https://blog.csdn.net/zpoison/article/details/80729052
6、运行一个自定义nginx站点,熟练配置步骤
#创建站点配置文件 [root@web01 conf.d]$cd /etc/nginx/conf.d [root@web01 conf.d]$egrep -v '#|^$' default.conf > 01_test.shnne.com.conf [root@web01 conf.d]$cat 01_test.shnne.com.conf server { listen 80; server_name localhost; location / { root /usr/share/nginx/html; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } } [root@web01 conf.d]$gzip default.conf [root@web01 conf.d]$ls 01_test.shnne.com.conf default.conf.gz
#创建站点目录和默认首页 [root@web01 conf.d]$mkdir -p /usr/share/nginx/html/test [root@web01 conf.d]$cd /usr/share/nginx/html/test/ [root@web01 test]$echo "test.shnne.com" > index.html [root@web01 test]$ll total 4 -rw-r--r-- 1 root root 15 Dec 3 20:28 index.html [root@web01 test]$cat index.html
#修改配置文件 [root@web01 conf.d]$cat 01_test.shnne.com.conf server { listen 80; server_name test.shnne.com; charset utf-8 #注意中文乱码处理添加此项 location / { root /usr/share/nginx/html/test; index index.html index.htm; } }
#测试 [root@web01 conf.d]$curl -H test.shnne.com 10.0.0.7 #配置/etc/hosts解析 [root@web01 conf.d]$vi /etc/hosts 10.0.0.7 test.shnne.com [root@web01 conf.d]$tail -1 /etc/hosts 10.0.0.7 test.shnne.com