主配置,nginx.conf
#user nobody; #工作的子进程数量(通常等于CPU数量或者2倍于CPU) worker_processes 4; #错误日志存放路径 #error_log logs/error.log; #error_log logs/error.log notice; error_log logs/error.log info; #指定pid存放文件 pid logs/nginx.pid; worker_rlimit_nofile 51200; events { #使用网络IO模型linux建议epoll,FreeBSD建议采用kqueue,window下不指定。 #use epoll; #允许最大连接数 worker_connections 51200; } http { include 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 D:/hwy/nginx-1.8.0/logs/access.log main; client_header_timeout 3m; client_body_timeout 3m; send_timeout 3m; client_header_buffer_size 1k; large_client_header_buffers 4 4k; sendfile on; tcp_nopush on; tcp_nodelay on; #keepalive_timeout 0; keepalive_timeout 120; #负载均衡 upstream aTestServer { server 127.0.0.1:8080; } upstream bTestServer { server 127.0.0.1:8080; } #包含所有的虚拟主机的配置文件[全路径] include D:/nginx/conf/vhosts/*.conf; # HTTPS server # #server { # listen 443 ssl; # server_name localhost; # ssl_certificate cert.pem; # ssl_certificate_key cert.key; # ssl_session_cache shared:SSL:1m; # ssl_session_timeout 5m; # ssl_ciphers HIGH:!aNULL:!MD5; # ssl_prefer_server_ciphers on; # location / { # root html; # index index.html index.htm; # } #} }虚拟域名,a_test.conf
server{ listen 80; server_name www.a_test.com; index index.html index.htm; root D:/nginx/html/a/; #配置首页精确匹配 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://aTestServer; } #配置首页 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://aTestServer; } #动态页面交给http://127.0.0.1:8080,也即我们之前在nginx.conf定义的upstream aTestServer 均衡 location ~ .*\.(php|jsp|cgi)?$ { 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://aTestServer; } #配置静态资源前缀匹配 location ~ ^/(fileUpload|doc)/ { root D:/nginx/html/a/; access_log off; expires 4d; } #配置Nginx动静分离,定义的静态页面直接从项目指定目录读取。 location ~ .*\.(html|htm|gif|jpg|jpeg|bmp|png|ico|txt|js|css)$ { access_log off; expires 30d; } #定义Nginx输出日志的路径 access_log D:/nginx/logs/aTest/access.log main; error_log D:/nginx/logs/aTest/error.log crit; }a_test的index.html
<!DOCTYPE html> <html> <head> <title>静动分离测试</title> </head> <link rel="stylesheet" type="text/css" href="./css/main.css"> <body> <img src="./img/senma.jpg" /> <button id="btn">请求</button> <h3></h3> </body> <script type="text/javascript" src='./js/jquery.js'></script> <script type="text/javascript"> $('#btn').on('click',function(){ $.ajax({ url:'api/main.php', data:{id:1}, type:'POST', dataType:'json', success:function(res) { alert(res.data); }, }) }) </script> </html>请求的main.php
<?php $id = $_POST['id']; if($id == 1){ $data = '我是A'; }elseif($id == 2){ $data = '我是B'; }else{ $data = '错误'; } echo json_encode(array('data'=>$data)); ?>apache目录指向D:/nginx/html/a/
nginx80端口,apache8080,端口
