nginx文件路径
- 主程序文件:/usr/sbin/nginx
$ which nginx /usr/sbin/nginx
使用 which 查看命令对应的执行文件的全路径是一种常用的方法
全局配置文件: /etc/nginx/nginx.conf
站点的配置文件: /etc/nginx/sites-enabled/default
错误日志文件 : /var/log/nginx/error.log
访问日志文件 :/var/log/nginx/access.log
日志文件的位置可以在 /etc/nginx/nginx.conf文件中配置
access_log /var/log/nginx/access.log; ## 访问日志文件路径名 error_log /var/log/nginx/error.log; ## 访问日志文件错误路径名
nginx 常用的命令
sudo apt-get update // update aptsudo apt-get install nginx // install nginx// 可使用systemctl 或者 servicesudo systemctl start nginx // start nginxsudo systemctl stop nginx // sotp nginxsudo systemctl status nginx // monitor nginx statussudo systemctl reload nginx // reload nginxps -ef | grep nginx // inqure nginx progress// 启动nginxnginxnginx -s stopnginx -s restartnginx -t // 检查nginx 配置
文件路径
default 服务主机配置文件,路径为:/etc/nginx/sites-available/default
#主机server {listen 80;server_name www.demo.com;root /home/zxl/wwwroot/demo/;index index.php index.html index.htm;#如果没有可访问目录或文件if (!-e $request_filename) {#将访问路径跳转至根目录下的index.php接受处理rewrite ^/(.*)$ /index.php/$1 last;break;}#处理请求路径满足匹配 .php 的响应# “ location ~ \.php ” == “ location ~ \.php($|/) ”#第一句的意思是如果请求字符中匹配到“ .php ”字符,就交给php解析器处理#第二句的意思是如果请求字符中匹配到以“ .php ”字符为结尾的或 “ .php/ ” 字符,就交给php解析器处理#推荐采纳第二句location ~ \.php($|/){#响应请求处理入口,使用php-fpm进行管理fastcgi_pass unix:/var/run/php5-fpm.sock;fastcgi_index index.php;fastcgi_split_path_info ^(.+\.php)(.*)$;fastcgi_param PATH_INFO $fastcgi_path_info;fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;include fastcgi_params;}#配置静态图片文件客户端缓存时间location ~ .*\.(gif|jgp|jpeg|png|bmp|swf)${expires 30d; #30天}#配置js、css文件客户端缓存时间location ~ .*\.(js|css)?${expires 1h; #1小时}#设置访问日志保存格式#log_format access '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer"' '"$http_user_agent" $http_x_forwarded_for';#设置访问日志保存路径#access_log /var/log/nginx/access.log access;}
nginx.conf 文件说明
#使用的用户和组user www-data;#指定工作衍生进程数(一般等于CPU总核数或总核数的两倍)worker_processes 4;#指定PID存放的路径pid /run/nginx.pid;#指定文件描述符数量worker_rlimit_nofile 51200;events {#使用的网络I/O模型,linux戏台推荐采用epoll模型,freebsd系统采用kqueue模型use epoll;#允许最大连接数worker_connections 51200;# multi_accept on;}http {### 基础设置##sendfile on;tcp_nopush on;tcp_nodelay on;keepalive_timeout 65;types_hash_max_size 2048;# server_tokens off;server_names_hash_bucket_size 128;# server_name_in_redirect off;include /etc/nginx/mime.types;default_type application/octet-stream;### 日志设置###指定错误日志存放路径数, 错误日志记录的级别可选项为:[debug|info|notice|warn|error|crit]access_log /var/log/nginx/access.log;error_log /var/log/nginx/error.log;### 压缩设置##gzip on;gzip_disable "msie6";gzip_vary on;# gzip_proxied any;gzip_comp_level 2;gzip_buffers 4 16k;gzip_http_version 1.1;gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;### nginx-naxsi config### Uncomment it if you installed nginx-naxsi###include /etc/nginx/naxsi_core.rules;### nginx-passenger config### Uncomment it if you installed nginx-passenger###passenger_root /usr;#passenger_ruby /usr/bin/ruby;### 虚拟主机设置##include /etc/nginx/conf.d/*.conf;include /etc/nginx/sites-enabled/*;}#mail {# # See sample authentication script at:# # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript## # auth_http localhost/auth.php;# # pop3_capabilities "TOP" "USER";# # imap_capabilities "IMAP4rev1" "UIDPLUS";## server {# listen localhost:110;# protocol pop3;# proxy on;# }## server {# listen localhost:143;# protocol imap;# proxy on;# }#}
什么是反向代理
当我们有一个服务器集群,并且服务器集群中的每台服务器的内容一样的时候,同样我们要直接从个人电脑访问到服务器集群服务器的时候无法访问,必须通过第三方服务器才能访问集群
这个时候,我们通过第三方服务器访问服务器集群的内容,但是我们并不知道是哪一台服务器提供的内容,此种代理方式称为反向代理。
什么是负载均衡
公司会建立很多的服务器,这些服务器组成了服务器集群,然后,当用户访问网站的时候,先访问一个中间服务器,再让这个中间服务器在服务器集群中选择一个压力较小的服务器,然后将该访问请求引入选择的服务器。
所以,用户每次访问,都会保证服务器集群中的每个服务器压力趋于平衡,分担了服务器压力,避免了服务器崩溃的情况。
一句话:nginx会给你分配服务器压力小的去访问
**
proxy_pass
nginx反向代理主要通过proxy_pass来配置,将你项目的开发机地址填写到proxy_pass后面,正常的格式为proxy_pass URL即可
server {listen 80;location / {proxy_pass http://10.10.10.10:20186;}}
Upstream模块实现负载均衡
- ip_hash指令
- server指令
- upstream指令及相关变量
上面写的三个指令,我们直接通过代码来一一分析
// 修改nginx.confworker_processes 1;events {worker_connections 1024;}http {upstream firstdemo {server 39.106.145.33;server 47.93.6.93;}server{listen 8080;location / {proxy_pass http://firstdemo;}}}
上面修改的nginx.conf就是上图中花圈的那个文件,nginx配置的主要修改就在这里。化繁为简,把原本nginx.conf里的内容直接替换为上面的不到20行的代码了. 既然不到20行,那就把里面对应的内容统统解释一下吧,有个了解就好
- worker_processes
- 工作进程数,和CPU核数相同
- 工作进程数,和CPU核数相同
- worker_connections
- 每个进程允许的最大连接数
- 每个进程允许的最大连接数
- upstream模块
- 负载均衡就靠它
- 语法格式:upstream name {}
- 里面写的两个server分别对应着不同的服务器
- 负载均衡就靠它
- server模块
- 实现反向代理
- listen监督端口号
- location / {}访问根路径
- proxy_pass http://firstdemo,代理到firstdemo里两个服务器上
- 实现反向代理
上面修改了nginx.conf之后,别忘了最重要的一步重启nginx
不过,更应该做到的是当用户第一次访问到其中一台服务器后,下次再访问的时候就直接访问该台服务器就好了,不用总变化了。那么就发挥了ip_hash的威力了。
// 省略...upstream firstdemo {ip_hash;server 39.106.145.33;server 47.93.6.93;}
ip_hash它的作用是如果第一次访问该服务器后就记录,之后再访问都是该服务器了,这样比如第一次访问是33服务器,那之后再访问也会分配为33服务器访问了
