Nginx动静分离架构
Nginx动静分离介绍
- Nginx动静分离把动态跟静态请求分开,nginx专门处理静态页面,而tomcat、resin、PHP、ASP处理动态页面。并不是只把动态和静态物理分离。
- 动静分离从目前实现的角度来讲大致分为两种
- 一种是纯粹的把静态文件独立成单独的域名,放在独立的服务器上,这种是主流的堆崇方案;
- 另一种是动态跟静态混合在一起发布,通过nginx来分开。
#虚拟机配置
[root@nginx ~]# more /usr/local/nginx/conf/domains/v1.discuz.net
server {
listen 80;
server_name v1.discuz.com;
location / {
root /usr/local/nginx/html/discuz;
index index.php index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
location ~ \.php$ {
root /data/webapps/discuz;
fastcgi_pass 192.168.21.11:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
#nginx主配文件配置
[root@nginx ~]#grep -v "#" /usr/local/nginx/conf/nginx.conf | grep -v "^$"
worker_processes 4;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
include domains/*;
}
[root@nginx ~]#
#两台设备分别都放一份发布目录;nginx用于发布静态网页,php-fpm用于发布动态网页
[root@nginx discuz]# ls
50x.html crossdomain.xml index.bak plugin.php source
admin.php data index.html portal.php static
api Discuz_X3.5_SC_UTF8_20221221.zip index.php qqqun.png template
api.php favicon.ico install readme uc_client
archiver forum.php LICENSE readme.html uc_server
config group.php member.php robots.txt upload
connect.php home.php misc.php search.php
[root@nginx discuz]#
[root@phpfpm discuz]# ls
50x.html crossdomain.xml index.bak plugin.php source
admin.php data index.html portal.php static
api Discuz_X3.5_SC_UTF8_20221221.zip index.php qqqun.png template
api.php favicon.ico install readme uc_client
archiver forum.php LICENSE readme.html uc_server
config group.php member.php robots.txt upload
connect.php home.php misc.php search.php
[root@phpfpm discuz]#
nginx动静分离示例配置文件内容
user www www;
worker_processes 8;
worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000 10000000;
pid /usr/local/nginx/nginx.pid;
worker_rlimit_nofile 102400;
events
{
use epoll;
worker_connections 102400;
}
http
{
include mime.types;
default_type application/octet-stream;
FastCGI_intercept_errors on;
charset utf-8;
server_names_hash_bucket_size 128;
client_header_buffer_size 4k;
large_client_header_buffers 4 32k;
client_max_body_size 300m;
sendfile on;
tcp_nopush on;
keepalive_timeout 60;
tcp_nodelay on;
client_body_buffer_size 512k;
proxy_connect_timeout 5;
proxy_read_timeout 60;
proxy_send_timeout 5;
proxy_buffer_size 16k;
proxy_buffers 4 64k;
proxy_busy_buffers_size 128k;
proxy_temp_file_write_size 128k;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.1;
gzip_comp_level 2;
gzip_types text/plain application/x-javascript text/css application/xml;
gzip_vary on;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" $request_time';
upstream jvm_web1 {
server 192.168.149.130:8080 weight=1 max_fails=2 fail_timeout=30s;
server 192.168.149.130:8081 weight=1 max_fails=2 fail_timeout=30s;
}
include vhosts.conf;
}
server
{
listen 80;
server_name www.jf1.com;
index index.jsp index.html index.htm;
root /data/webapps/www1;
location /
{
proxy_next_upstream http_502 http_504 error timeout invalid_header;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://jvm_web1;
#非PHP请求就要把fastcgi_pass改为proxy_pass
}
#匹配以{php、jsp、cgi、shtml}后缀的动态页面请求,然后将请求根据proxy_pass转发到后端服务器
location ~ .*\.(php|jsp|cgi|shtml)?$
{
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://jvm_web1;
}
#匹配以{html、htm、gif、jpg、jpeg、bmp、png、ico、txt、js、css}后缀的静态页面请求
#匹配到的请求为本地请求
location ~ .*\.(html|htm|gif|jpg|jpeg|bmp|png|ico|txt|js|css)$
{
root /data/webapps/www1;
expires 30d;
}
access_log /data/logs/jvm_web1/access.log main;
error_log /data/logs/jvm_web1/error.log crit;
}
检查nginx动静分离是否成功
- 在后端服务器中启动相应的服务及端口,然后删除后端服务器上的某个静态文件,测试访问的页面是否正常显示;如果可以访问并正常显示说明静态资源nginx是直接返回,否则证明动静分离不成功。
Nginx Location字段剖析
- nginx是通过查找配置文件将客户端的请求映射到一个location block,而location是nginx配置中的一个指令,用于访问的URL匹配,而在这个location中所配置的每个指令将会启动不同的模块去完成相应的工作。
- 默认nginx.conf配置文件中至少存在一个location /,即表示客户端浏览器请求的URL为:域名+/,如果location/newindex/,则表示客户端浏览器请求的URL为:域名+/newindex/。
常见的location匹配URL的方式:
匹配的方式 | 含义 |
---|---|
= | 字面精确匹配 |
^~ | 最大前缀匹配 |
/ | 不带任何前缀:最大前缀匹配 |
~ | 大小写相关的正则匹配 |
~* | 大小写无关的正则匹配 |
@ | location 内部重定向的变量 |
- location=、^~、/属于普通字符 串匹配,location、* 属于正则表达式 匹配,location优先级与在nginx.conf配置文件中的先后顺序无关。
- location= 精确匹配会第一个被处理,如果发现精确匹配,nginx则停止其他location的匹配
- 普通字符匹配,正则表达式规则和完整URL规则将被有限和查询匹配,^~ 为最大前缀匹配,如果匹配到该规则,nginx则停止搜索其他location的匹配,否则nginx会继续匹配其他级别的location指令。
- 正则匹配**、*** ,如果找到相应的匹配,nginx则停止搜索其他location的匹配,当没有正则表达式或者没有正则表达式被匹配的情况下,那么匹配程序最高的逐字匹配指令会被使用。
location规则匹配优先级
(location=) > (location 完整路径) > (location ^~ 路径) > (location ~ | ~* 正则顺序) > (location 部分起始路径) > (location /)