nginx 以小巧,并发能力强而著称,其运行速度非常快,据说达到10倍以上, 国内京东,新浪等大型网站都以此作为web服务器
一. 安装
wget http://nginx.org/download/nginx-1.9.2.tar.gz
编译 nginx ,V1.9.0版本支持TCP负载均衡,需在编译时开启TCP负载选项。
./configure --with-stream
编译过程中,容易报库缺失错误:
./configure: error: the HTTP rewrite module requires the PCRE library.
yum -y install pcre-devel
./configure: error: the HTTP gzip module requires the zlib library
yum install -y zlib-devel
二. 测试
启动 nginx: ./usr/local/nginx/sbin/nginx
停止 nginx: ./nginx -s stop
关闭 nginx: ./nginx -s quit
重新加载配置:./nginx -s reload
http://localhost 显示nginx的欢迎页
三. 常用配置
3.1 当没有索引页时,显示文件和目录列表
/usr/local/nginx/conf/nginx.conf
worker_processes 1; #服务进程数量,一般等于CPU数量 events { useepoll; #linux上使用epoll网络模型,可不配 worker_connections 1024; #一个worker_processe允许的最近并发连接数量 } location / { root html; index index.html index.htm; autoindex on; # 显示文件列表 autoindex_exact_size on; autoindex_localtime on; } #配置/ios目录下后缀.plist结尾文件以文本方式显示,避免下载 location ~ .*/ios/.*\.plist$ { add_header Content-Type text/plain; }
3.2 配置虚拟目录
nginx 好像没有虚拟目录这个概念,但是可以通过指定请求路径时的访问路径实现, 具体通过 alias 和 root 这两个参数设置,这两个参数非常容易混淆,导致出现404错误
现在要访问: /home/scada/www/test 下 hello.html 这个文件, 如下配置:
# alias 实现 location ^~/test/ { alias /home/scada/www/test/; } # root 实现 location ^~/test/ { root /home/scada/www; }
浏览器访问: http://localhost/test/hello.html
3.3 配置负载均衡
Tomcat 可使用Apache mod_jk 组件方式实现负载均衡,但是配置起来比较麻烦。 现在可以直接使用nginx实现负载均衡,只添加少许配置即可实现。
现有 192.168.5.154 192.168.5.155 192.168.5.158 三台服务器,以192.168.5.154 作为 nginx 前端主服务器,192.168.5.155,192.168.5.158 作为Tomcat服务器。
http { #负载均衡服务器列表,可设置不同的权重 upstream localhost { server 192.168.5.155:8080 weight=5; server 192.168.5.158:8080 weight=5; } server { location / { proxy_pass http://localhost; #反向代理 } } }
访问:http://192.168.5.154 ,发现显示Tomcat欢迎页,多次刷新,显示不同的欢迎页面
四. 运行 cgi 服务
nginx 是不支持直接运行 cgi 程序的, 但支持 fastcgi, 当 ngix 接收到请求后, 通过TCP/unix domain方式与fastcgi进程管理器通信。
a. 编写 fastcgi 方式的python服务
WSGI 是 python应用程序或框架与web服务器之间的一种接口, 具体实现有 tornado,flup 等, 这里使用 flup
下载flup: https://pypi.python.org/pypi/flup/1.0.2#downloads
安装flup: python setup.py install
#!/usr/bin/python #encoding: utf-8 from flup.server.fcgi import WSGIServer def myapp(envrion,start_response): start_response("200 OK",[("Content-Type","text/plain")]) return ["Hello world\n"] if __name__=='__main__': WSGIServer(myapp,bindAddress=("127.0.0.1",8008)).run() # 监听8008端口
b. 启动fastcgi服务: sudo ./hello.py --method=prefork/threaded minspare=50 maxspare=50 maxchildren=1000
c. 配置nginx,支持fastcgi
location / { root html; index index.html index.htm; fastcgi_pass 127.0.0.1:8008; #指定8008,与wsgi服务保持一致 fastcgi_param SCRIPT_FILENAME ""; fastcgi_param PATH_INFO $fastcgi_script_name; include fastcgi.conf; # 导入fastcgi配置 }
浏览器访问: http://localhost/hello.py 显示hello world
相关推荐
由于生成的证书是jks格式,nginx不能直接用,需要要转成PEM格式,这要用到jks2pfx工具进行转换。 jks2pfx的命令格式:JKS2PFX.bat keystore password alias exportname keystore:KeyStore文件绝对路径 password:...
Nginx (engine x) 是一个高性能的HTTP和反向代理web服务器,同时也提供了IMAP/POP3/SMTP服务。
包含lvs+nginx负载均衡 nginx安装手册 nginx使用文档 keepalived高可用高可用方法 SwitchHosts软件包 还有相关安装包
关于nginx+tomcat 的配置文件,及详细介绍
nginx使用指南.pdf
nginx使用手册.chm 配置服务器nginx的手册,参考工具,欢迎下载
nginx -t -c F:/nginx-1.12.2/conf/nginx.conf 测试nginx配置文件是否正确 start nginx nginx -s stop nginx -s quit nginx -s reload nginx -t 修改后的hosts文件放入c:\windows\system32\drivers\etc替换以前的...
Linux自动化脚本安装Nginx使用说明,里面带了所需要的文件与文档说明
nginx使用案例、Nginx小试
最近碰到一个问题就是nginx转发到另一个nginx使用域名不通报502,通过nginx的日志发现如下 10.220.53.117 – – [18/Feb/2020:10:26:07 +0800] “POST /test/v1/api/f1 HTTP/1.1” 502 173 “-” “okhttp/3.11.0”...
Nginx配置SSL,Nginx使用SSL模块配置https支持
Linux运维-d2企业级Nginx使用-01基于域名的日志分割.mp4
某项目前端资源,可结合nginx使用
HETF-Nginx 使用入门手册.docx
软件测试面试题介绍-Nginx使用场合介绍
tomcat结合nginx使用小结.pdf
HETF-Nginx 使用入门手册.zip
nginx使用方法 ,基本使用和配置 配置文件的修改和参数的添加