最近学习nginx先拿windos练练手,稍微有些积累记录下
1. 先去网上下载nginx后解压,我这边放到F:nginx下了;
2. 点击nginx目录下想nginx.exe按照,闪了一下;
3. 打开浏览器,输入localhost看看是否有welcome to nginx字样;或是打开任务管理器,进程中是否有nginx.exe或是打开 nginx的安装目录看logs下面的error.log大小是否是0kb;
4. 如果能正常启动则继续,如果启动不了看看error.log提示内容是什么; 因为nginx默认监听80端口,可能会出现端口占用的情况, 改下conf中的nginx.conf文件就好,在
http{
server{
listen 8077; # 监听8077端口
}
}下面会介绍;我在启动的时候,因为安装了php,所以发生了端口占用这样的事情;
5. 因为是测试本地的服务,所以修改hosts,新增 127.0.0.1 a.com;
6. 我在本地和外网准备了2台服务器;
7. 修改修改下tomcat的server.xml文件,除了配置端口等外,在
<Host> <Context path="" docBase="/zjlm.client.web" reloadable="true"/> </Host>
8. 完整的nginx.conf文件内容是
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
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 logs/access.log main;
server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
upstream a.com {
server 60.205.141.136:8080 weight=1; # 服务器1
server 127.0.0.1:8087 weight=1; #服务器2
}
server {
listen 8077; #监听端口
server_name a.com
root F:/nginx/nginx/nginx/html;
proxy_redirect off;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.jsp index.html index.htm;
#设置主机头和客户端真实地址,以便服务器获取客户端真实IP
proxy_set_header Host $host;
#防止ajax安全请求问题
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
#禁用缓存
proxy_buffering off;
#设置反向代理的地址
proxy_pass http://a.com;
}
location ~ \.(jsp|jspx|do|action)(\/.*)?$ {
index index.jsp;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_pass http://a.com;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ {
root F:/nginx/nginx/nginx/html;
}
location ~ .*\.(html|js|css|png|gif)$ {
root F:/nginx/nginx/nginx/html;
}
#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 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;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# 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;
# }
#}
}
9. 需要注意的事情, 一开始配置完静态文件不能访问,静态资源路径不对, 需要把静态资源拷到nginx目录下,然后配置root 文件目录;
10. 访问a.com:8077查看情况
11. #注* server段 proxy_pass定义的a.com需要跟upstream 里面定义的a.com一致,否则server找不到均衡。
12. #配置Nginx动静分离
location ~ .*\.(html|htm|gif|jpg|jpeg|bmp|png|ico|txt|js|css)$ {
root F:/nginx/nginx/nginx/html;
}
13. #expires定义用户浏览器缓存的时间为3天,如果静态页面不常更新,可以设置更长,这样可以节省带宽 和缓解服务器的压力
expires 3d;
14. 开启nginx的监控
nginx简单状态监控,在nginx.conf中添加如下代码即可监控nginx当前的状态,然后访问http://serverip/status即可访问
location /status {
stub_status on;
access_log off;
}
一般显示为
Active connections: 16
server accepts handled requests
191226 191226 305915
Reading: 0 Writing: 1 Waiting: 15
ctive connections: 对后端发起的活动连接数.
Server accepts handled requests: Nginx总共处理了24个连接,成功创建24次握手(证明中间没有失败的),总共处理了129个请求.
Reading: Nginx 读取到客户端的Header信息数.
Writing: Nginx 返回给客户端的Header信息数.
Waiting: 开启keep-alive的情况下,这个值等于 active – (reading + writing),意思就是Nginx已经处理完成,正在等候下一次请求指令的驻留连接.
注意的,本模块默认是不会编译进Nginx的,如果你要使用该模块,则要在编译安装Nginx时指定: