nginx 新手入门
Nginx 是一个高性能的http 和反向代理服务器,也是一个代理服务器。
Nginx比Apache 更加轻量级,占用的资源少,抗并发,二apache是阻塞型的,在高并发下,nginx更占优势。
我们先从官网上面下载一个windows版本的nginx。http://nginx.org/en/download.html
配置文件:解压后,找到nginx.conf 我下载的是1.8
#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; server { listen 80; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / { root 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 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; # } #} }
nginx 集群的配置
#######################################所有的指定必须由分号结束####################################### #user nobody; ######### 配置用户或者组 worker_processes 1; ######## 允许生成的进程数 #error_log logs/error.log; #error_log logs/error.log notice; ########### 制定日志路径,级别。这个设置可以放入全局块,http块,server块,级别以此为:debug|info|notice|warn|error|crit|alert|emerg #error_log logs/error.log info; #pid logs/nginx.pid; ############ 指定运行文件存放的地址 events { accept_mutex on; ########## 设置网路连接序列化,防止惊群现象发生,默认为on multi_accept on; ######### 设置一个进程是否同时接受多个网络连接,默认为off #use epoll; ########## 事件驱动模型,select|poll|kqueue|epoll|resig|/dev/poll|eventport worker_connections 1024; ############ 最大连接数 } http { include mime.types; ########文件扩展名与文件类型映射表 default_type application/octet-stream; ########默认文件类型,默认为text/plain #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; ############# combined为日志格式的默认值 sendfile on; ############### 允许sendfile方式传输文件,默认为off,可以在http块,server块,location块。 #tcp_nopush on; #sendfile_max_chunk 100k; ################# 每个进程每次调用传输数量不能大于设定的值,默认为0,即不设上限。 #keepalive_timeout 0; keepalive_timeout 65; ############### keepalive_timeout 65; #连接超时时间,默认为75s,可以在http,server,location块。 #gzip on; upstream myservice { server 127.0.0.1:7878 weight=1; server 192.168.10.121:3333 weight=1; #热备 #ip_hash; ####### nginx会让相同的客户端ip请求相同的服务器 解决session共享的问题 } #error_page 404 https://www.baidu.com; ####### 错误页 #proxy_intercept_errors on; ######## 如果被代理服务器返回的状态码为400或者大于400,设置的error_page配置起作用。默认为off。 #proxy_method get; ######## 支持客户端的请求方法。post/get; #proxy_http_version 1.0 ; ####### Nginx服务器提供代理服务的http协议版本1.0,1.1,默认设置为1.0版本 #proxy_connect_timeout 1; ###### nginx服务器与被代理的服务器建立连接的超时时间,默认60秒 #proxy_read_timeout 1; ######## nginx服务器想被代理服务器组发出read请求后,等待响应的超时间,默认为60秒。 #proxy_send_timeout 1; ####### nginx服务器想被代理服务器组发出write请求后,等待响应的超时间,默认为60秒。 server { keepalive_requests 120; ######## 单连接请求上限次数。 listen 80; ######## 监听端口 server_name localhost; ######## 监听地址 #charset koi8-r; #access_log logs/host.access.log main; location / { ###### 请求的url过滤,正则匹配,~为区分大小写,~*为不区分大小写。 root html; #### 根目录 index index.html index.htm; ##### 设置默认页 proxy_pass http://myservice; ###### 请求转向mysvr 定义的服务器列表 #deny 127.0.0.1; ##### 拒绝的ip #allow 172.18.5.54; ##### 允许的ip } #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; # } #} }
nginx的启动和关闭(*****nginx启动不像tomcat启动有弹框,刚开始时,容易入这个坑,以为没有启动******)
nginx 关闭 使用windows 的时候出现很多的服务 可以使用这个关闭 taskkill /F /IM nginx.exe
本人建议使用命令的方式进行启动
启动Nginx:start nginx
快速停止或关闭Nginx:nginx -s stop
正常停止或关闭Nginx:nginx -s quit
配置文件修改重装载命令:nginx -s reload
上面会引发session共享的问题