Nginx (“engine x”) 是一个高性能的HTTP和 反向代理 服务器,也是一个 IMAP/POP3/SMTP 服务器。 Nginx 是由 Igor Sysoev 为俄罗斯访问量第二的 Rambler.ru 站点开发的,第一个公开版本0.1.0发布于2004年10月4日。其将源代码以类BSD许可证的形式发布,因它的稳定性、丰富的功能集、示例配置文件和低系统资源的消耗而闻名。2011年6月1日,nginx 1.0.4发布。
Nginx是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,并在一个BSD-like 协议下发行。由俄罗斯的程序设计师Igor Sysoev所开发,供俄国大型的入口网站及搜索引擎Rambler(俄文:Рамблер)使用。其特点是占有内存少,并发能力强,事实上nginx的并发能力确实在同类型的网页服务器中表现较好,中国大陆使用nginx网站用户有:百度、新浪、网易、腾讯等。
本地系统:Ubuntu 14.04.2,安装命令:
apt-get install nginx#需要root权限
自动安装结束
root@iZ23goxo66aZ:/usr/share/nginx# whereis nginx #输出以下信息 /usr/sbin/nginx #命令 /etc/nginx #配置文件目录 /usr/share/nginx #html所在目录,日志也可以指定这里 /usr/share/man/man1/nginx.1.gz #man操作手册
并且已经帮我们启动nginx,访问如图:
root@iZ23goxo66aZ:~# nginx -h #查看帮助 nginx version: nginx/1.4.6 (Ubuntu) #当前版本 Usage: nginx [-?hvVtq] [-s signal] [-c filename] [-p prefix] [-g directives] Options: -?,-h : this help -v : show version and exit -V : show version and configure options then exit -t : test configuration and exit -q : suppress non-error messages during configuration testing -s signal : send signal to a master process: stop, quit, reopen, reload -p prefix : set prefix path (default: /usr/share/nginx/) -c filename : set configuration file (default: /etc/nginx/nginx.conf) -g directives : set global directives out of configuration file
root@iZ23goxo66aZ:~# nginx -v #查看版本 nginx version: nginx/1.4.6 (Ubuntu)
root@iZ23goxo66aZ:~# nginx -V #查看版本以及可配置参数 nginx version: nginx/1.4.6 (Ubuntu) built by gcc 4.8.4 (Ubuntu 4.8.4-2ubuntu1~14.04) TLS SNI support enabled configure arguments: --with-cc-opt='-g -O2 -fstack-protector --param=ssp-buffer-size=4 -Wformat -Werror=format-security -D_FORTIFY_SOURCE=2' --with-ld-opt='-Wl,-Bsymbolic-functions -Wl,-z,relro' --prefix=/usr/share/nginx --conf-path=/etc/nginx/nginx.conf --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log --lock-path=/var/lock/nginx.lock --pid-path=/run/nginx.pid --http-client-body-temp-path=/var/lib/nginx/body --http-fastcgi-temp-path=/var/lib/nginx/fastcgi --http-proxy-temp-path=/var/lib/nginx/proxy --http-scgi-temp-path=/var/lib/nginx/scgi --http-uwsgi-temp-path=/var/lib/nginx/uwsgi --with-debug --with-pcre-jit --with-ipv6 --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module --with-http_addition_module --with-http_dav_module --with-http_geoip_module --with-http_gzip_static_module --with-http_image_filter_module --with-http_spdy_module --with-http_sub_module --with-http_xslt_module --with-mail --with-mail_ssl_module
root@iZ23goxo66aZ:~# nginx -t #测试nginx配置文件是否正确 nginx: the configuration file /etc/nginx/nginx.conf syntax is ok #is ok说明配置文件正确 nginx: configuration file /etc/nginx/nginx.conf test is successful #测试通过
root@iZ23goxo66aZ:~# nginx -s reload #更新配置文件后,不需要重启nginx,直接重新加载配置文件
root@iZ23goxo66aZ:~# nginx -s stop #关闭nginx
root@iZ23goxo66aZ:~# nginx #启动nginx
root@iZ23goxo66aZ:/etc/nginx# cp nginx.conf nginx.conf.bak.20160120 #备份配置文件,但是基本不修改这个配置文件
root@iZ23goxo66aZ:/etc/nginx# vim /etc/nginx/sites-enabled/default #基本会修改这个配置文件配置跳转和负载均衡,或者新建配置文件在sites-available中,再在sites-enabled中创建软链接
gxx@iZ23goxo66aZ:~/sbin$ ps -ef|grep nginx #看到nginx有一个主进程,4个worker进程,可以从nginx.conf中看到worker_processes 配置为4 root 14247 1 0 19:35 ? 00:00:00 nginx: master process nginx www-data 14248 14247 0 19:35 ? 00:00:00 nginx: worker process www-data 14249 14247 0 19:35 ? 00:00:00 nginx: worker process www-data 14250 14247 0 19:35 ? 00:00:00 nginx: worker process www-data 14251 14247 0 19:35 ? 00:00:00 nginx: worker process gxx 14255 14021 0 19:35 pts/0 00:00:00 grep --color=auto nginx
server {#简单的端口监听并跳转 listen 80; server_name 121.43.104.34; access_log logs/localhost.access.log;#日志路径 location / { root /usr/share/nginx/html; } location /jf189/ { proxy_pass http://121.43.104.34:8080;#这里使用localhost会跳转失败 } location /config_center/ { proxy_pass http://121.43.104.34:9090;#这里使用localhost会跳转失败 } }
#负载分发策略分:[轮询(默认)],[ip_hash],[加权轮询],[fair(第三方)],[url_hash(第三方)],后四种可以选择一个配置,否则默认为第一种 upstream mine{ # ip_hash;#[ip_hash]:每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session的问题。 server 121.43.104.34:9090 weight=1;#[加权轮询]:可以配置多台,负载均衡,weight设置权重(不设置默认为1) 不知道为啥这里使用localhost会跳转失败 server 121.43.104.34:8080 weight=1;#[加权轮询]:可以配置多台,负载均衡,weight设置权重(不设置默认为1) 不知道为啥这里使用localhost会跳转失败 # fair;#[fair]:按后端服务器的响应时间来分配请求,响应时间短的优先分配。 # hash $request_uri;#[url_hash]:按访问url的hash结果来分配请求,使每个url定向到同一个后端服务器,后端服务器为缓存时比较有效。 # hash_method crc32;#[url_hash]:按访问url的hash结果来分配请求,使每个url定向到同一个后端服务器,后端服务器为缓存时比较有效。 #注意,负载均衡里配置的机器,如果有台挂了,将不再有请求分发到这台,直到这台启动起来 } #设置需要负载的网站ip或者域名 server { listen 80; server_name 121.43.104.34; location /{ proxy_pass http://mine; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } }