1.nginx的反向代理
互联网中直接将服务器暴露在外面是很危险的,一旦被攻破后果不堪设想。代理服务器的出现,保护了真实的服务器,可以说特别重要了。nginx就是一个代理服务器。客户端发来http请求,先经过nginx的转发给后端服务器处理后,再将结果返回给客户端。此时反向代理服务器和目标服务器对外就是一个服务器,暴露的是代理服务器地址,隐藏了真实服务器IP地址。

2.nginx还可以进行负载均衡
nginx可以向后端转发

3.nginx进行动态分离
静态文件可以有nginx处理,动态文件可以转发后端的php,java之类的服务器来处理

4.nginx的配置文件
配置文件分为全局块、events块、http块三大部分。

5.nginx的优点
1.高并发。静态小文件
2.占用资源少。2万并发、10个线程,内存消耗几百M。
3.功能种类比较多。web,cache,proxy。
4.支持epoll模型,使得nginx可以支持高并发。
5.nginx 配合动态服务,nginx——fastcgi 的方式运行PHP,jsp
6.利用nginx可以对IP限速,可以限制连接数。
7.配置简单,更灵活。

6.nginx的编译安装
./configure --prefix=/data/nginx-1.10.1 --user=nginx --group=nginx --with-http_ssl_module --with-http_stub_status_module
测试配置文件的正确性:/webservers/nginx -t
启动nginx :/webservers/nginx
进程里查看nginx
ps -ef|grep nginx
查看编译参数:
/webservers/nginx -V
重新平滑启动nginx:/webservers/nginx -s reload

7.nginx配置文件

worker_processes 1; #进程数
 events {
 worker_connections 1024; #每个进程最大连接数
 }
 http {
 include mime.types;
 default_type application/octet-stream;
 sendfile on;
 keepalive_timeout 65;
 server { #虚拟主机配置开始了,特备重要
 listen 80; #监听端口
 server_name localhost; #主机名称
 location / {
 root html; #设置根目录
 index index.html index.htm; #设置首页文件
 }
 error_page 500 502 503 504 /50x.html; #错误页面显示
 location = /50x.html {
 root html;
 }
 }
 }

8.nginx启动状态监控

location /status {
 stub_status on; #表示开启stubStatus的工作状态统计功能。
 access_log off; #access_log off; 关闭access_log 日志记录功能。
 auth_basic “status”; #auth_basic 是nginx的一种认证机制。
 auth_basic_user_file conf/htpasswd; #用来指定密码文件的位置。
 }

9.nginx负载均衡配置

upstream jcy.com{
 ip_hash;#保证一位用户固定访问一台主机
 server 192.168.0.108:5000 fail_timeout=2s max_fails=1;#后端服务器的两台主机
 server 192.168.0.106:5000 fail_timeout=2s max_fails=1;
 }server {
 listen 80;
 server_name 192.168.0.101;
location / {
	proxy_pass http://jcy.com;    #进行后端转发
	proxy_redirect default;
	proxy_connect_timeout 2s;
}

10.处理php请求的配置

server{
 listen 80;
 server_name www.jcy.com;
 root /webservers/nginx/html; # 准备存放代码工程的路径
 location / {
 index index.php; #跳转到www.example.com/index.php
 autoindex on;
 }
 #当请求网站下php文件的时候,反向代理到php-fpm
 location ~ .php${
 include /usr/local/etc/nginx/fastcgi.conf; #加载nginx的fastcgi模块
 fastcgi_intercept_errors on;
 fastcgi_pass 127.0.0.1:9000; #nginx fastcgi进程监听的IP地址和端口
 }
 }

11.nginx全局变量

nginx 学习教程 nginx基础知识_httpquery_string。
nginx 学习教程 nginx基础知识_nginx_02args,则该变量的值为"?",否则为""。
nginx 学习教程 nginx基础知识_nginx 学习教程_03content_type: 请求头中的Content-Type字段。
nginx 学习教程 nginx基础知识_php_04uri相同。
nginx 学习教程 nginx基础知识_php_05host: 请求主机头字段,否则为服务器名称。
nginx 学习教程 nginx基础知识_linux_06http_cookie: 客户端cookie信息。
nginx 学习教程 nginx基础知识_php_07request_method: 客户端请求的动作,通常为GET或POST。
nginx 学习教程 nginx基础知识_nginx_08remote_port: 客户端的端口。
nginx 学习教程 nginx基础知识_php_09request_body_file`: 客户端请求主体的临时文件名。
nginx 学习教程 nginx基础知识_nginx_10request_filename: 当前请求的文件路径,由root或alias指令与URI请求生成。
nginx 学习教程 nginx基础知识_nginx 学习教程_11 $scheme://example.comnginx 学习教程 nginx基础知识_http_12server_protocol: 请求使用的协议,通常是HTTP/1.0或HTTP/1.1。
nginx 学习教程 nginx基础知识_http_13server_name: 服务器名称。
nginx 学习教程 nginx基础知识_linux_14request_uri: 包含请求参数的原始URI,不包含主机名,如:/foo/bar.php?arg=baz,它无法修改。
nginx 学习教程 nginx基础知识_nginx_15uri不包含主机名,如/foo/bar.html可能和最初的值有不同,比如经过重定向之类的。它可以通过内部重定向,或者使用index指令进行修改。不包括协议和主机名,例如/foo/bar.html。