Nginx适用场景
HTTP的反向代理服务器
正向代理
使用代理服务器进行安全管理
反向代理
通常位于服务端开发中,客户端访问时不直接访问到服务器,而是访问到反向代理服务器,在后端多个服务器之间提供负载均衡,为服务慢的服务器提供缓存
Nginx提供反向代理的原理
正向+反向代理
动态静态资源分离
不分离会变慢
静态资源无需经过Tomcat,Tomcat只负责处理动态请求
例如:当后缀为gif的时候,Nginx会直接获取到当前请求文件并返回
Nginx本身作为静态资源服务器
例如当网站不大时不需要经常修改的静态网站,只需要直接利用Nginx访问即可,不需要运行Tomcat,拥有较好的性能
Nginx优点
高并发、高性能
可扩展性好
高可靠性
热部署
开源、可商业
Nginx安装
Linux的安装(主要)
yum install yum-utils
vim /etc/yum.repos.d/nginx.repo
输入源信息
yum list|grep nginx
yum install nginx 版本号
nginx -v
也可以手动安装:
解压
tar -zxvf nginx-1.22.0.tar.gz
执行配置文件
./configure
最后
make
Mac OS的安装
使用brew工具
brew install nginx
常用命令
/usr/sbin/nginx #启动
ps -aux |grep nginx #查看nginx是否启动,去浏览器访问80端口也可以
-h #帮助
-c filename #读取指定配置文件
nginx -c /etc/nginx/nginx.conf #以这个配置启动nginx,默认已经启动了
nginx -s stop #停止nginx 之后再运行上面就可以启动,适合在配置文件多时用
-t #检查配置文件有没有语法问题;显示当前生效的配置文件
-v #版本
-V #版本号和其他一些参数
whereis nginx #同下,系统命令
ps aux |grep nginx #找到正在运行的nginx路径
-s信号
stop 立即停止
quit 优雅停止,等待旧的服务执行完毕
reload 重启,内部使用优雅停止再启动
reopen 更换日志文件(不常用)
配置文件
语法
; 结尾
{} 组织多条指令
include 引入 include /etc/nginx/conf.d/*.conf
# 注释
$ 变量
nginx.conf
default.conf
场景应用:搭建一个静态文件的Nginx服务
上传静态资源
对Nginx原来的配置进行修改,
/usr/local/nginx/conf/nginx.conf
将location位置的路径进行修改;
重启./nginx -s reload
使用浏览器就可以直接访问到这些静态文件了!
日志查看
cat /etc/nginx/nginx.conf
可以看到有这样一个日志输出路径,打开它就可以看到日志内容
cat /var/log/nginx/accecc.log
搭建反向代理服务与配置负载均衡
访问nginx服务器,nginx会根据你配置的多个服务器权值自动连接到不同服务器
#全局配置
#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/location/proxy_pass中使用
upstream proxy_myservice{
# 服务器资源 负载均衡权值
server 144.xxxx.1:8080 weight=1
server 82.xxxx.1:8081 weight=2
server 82.xxxx.1:8082 weight=1
}
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
#反向代理指令
proxy_pass http://proxy_myservice
}
#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;
# }
#}
}