nginx学习笔记

    xiaoxiao2025-01-09  9

    nginx定义:http服务器,反向代理服务器,邮件服务器。 常用功能:http代理,反向代理;负载均衡;Web缓存。

    编辑安装命令:

    ./configuremakesudo make install

    查看进程:ps -ef | grep nginx 停止:./nginx -g TERM | INT | QUIT 重启:/usr/local/nginx/sbin/nginx -s reload 或者sudo kill -HUP 6394 检查:sudo /usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf

    模块化结构:

    核心模块

    标准http模块

    可选http模块

    邮件服务模块

    第三方模块

    请求处理机制:异步非阻塞和多进程机制 事件驱动机制:select库,poll库,epoll库,rtsig模型等 nginx服务器进程:

    主进程

    工作进程

    缓存索引重建及管理进程

    nginx后端服务器组的配置指令

    upstreamserverip_hashkeepaliveleast_conn

    负载均衡配置: 8081和8080对应两个不同端口号的tomcat

    upstream demo_site { server 127.0.0.1:8081 weight=1; server 127.0.0.1:8080 weight=1; } location /demo { proxy_pass http://demo_site; }

    使用proxy_cache 进行静态资源的缓存。

    经过测试,第一次请求静态资源会经过web应用,之后请求由nginx直接返回。

    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; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; #gzip on; 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; proxy_temp_path /var/tmp/temp_nginx; proxy_cache_path /var/cache/nginx/ levels=1:2 keys_zone=nginx_cache:100m inactive=1d max_size=10g; upstream demo_site { server 127.0.0.1:8081 weight=1; server 127.0.0.1:8080 weight=1; } server { listen 80; server_name localhost; #charset koi8-r; access_log logs/host.access.log main; location / { root html; index index.html index.htm; } location ~* ^.+\.(js|ico|gif|jpg|jpeg|png|html|htm)$ { access_log logs/access.log; proxy_next_upstream http_502 http_504 error timeout invalid_header; proxy_set_header Host $host; proxy_cache nginx_cache; proxy_cache_valid 200 302 12h; proxy_cache_key $host$uri$is_args%args; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $remote_addr; proxy_pass http://demo_site; expires 1d; } location /demo { proxy_pass http://demo_site; } #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; # } #} }
    转载请注明原文地址: https://ju.6miu.com/read-1295296.html
    最新回复(0)