正向代理(给客户端用的)
反向代理(给服务端用的)
安全(和正向功能类似)、后端多个服务器做负载均衡
正向+反向代理
动态静态资源分离静态资源无需经过 tomcat,tomcat 只负责处理动态请求
优点1、高并发,高性能
2、可扩展性好,模块化
3、高可靠性
4、热部署,在不重启的情况下更新
5、开源,可商用
yum install yum-utils
vi /etc/yum.repos.d/nginx.repo [nginx-stable] name=nginx stable repo baseurl=http://nginx.org/packages/centos/7/$basearch/ gpgcheck=1 enabled=1 gpgkey=https://nginx.org/keys/nginx_signing.key moudle_hosfixes=true [nginx-mainline] name=nginx mainline repo baseurl=http://nginx.org/packages/mainline/centos/7/$basearch/ gpgcheck=1 enabled=0 gpgkey=https://nginx.org/keys/nginx_signing.key moudle_hosfixes=true
yum list | grep nginx nginx.x86_64 1:1.20.2-1.el7.ngx nginx-stable nginx-debug.x86_64 1:1.8.0-1.el7.ngx nginx-stable nginx-debuginfo.x86_64 1:1.20.2-1.el7.ngx nginx-stable nginx-module-geoip.x86_64 1:1.20.2-1.el7.ngx nginx-stable nginx-module-geoip-debuginfo.x86_64 1:1.20.2-1.el7.ngx nginx-stable nginx-module-image-filter.x86_64 1:1.20.2-1.el7.ngx nginx-stable nginx-module-image-filter-debuginfo.x86_64 1:1.20.2-1.el7.ngx nginx-stable nginx-module-njs.x86_64 1:1.20.2+0.7.2-1.el7.ngx nginx-stable nginx-module-njs-debuginfo.x86_64 1:1.20.2+0.7.2-1.el7.ngx nginx-stable nginx-module-perl.x86_64 1:1.20.2-1.el7.ngx nginx-stable nginx-module-perl-debuginfo.x86_64 1:1.20.2-1.el7.ngx nginx-stable nginx-module-xslt.x86_64 1:1.20.2-1.el7.ngx nginx-stable nginx-module-xslt-debuginfo.x86_64 1:1.20.2-1.el7.ngx nginx-stable nginx-nr-agent.noarch 2.0.0-12.el7.ngx nginx-stable pcp-pmda-nginx.x86_64 4.3.2-13.el7_9 updates
yum install nginx 1:1.20.2-1.el7.ngx
nginx -v nginx version: nginx/1.20.2常用命令 启动
/usr/sbin/nginx停止
立即停止
nginx -s stop
优雅停止(线上)
nginx -s quit重启
使用的优雅重启
nginx -s reload指定配置文件
nginx -c /etc/nginx/nginx.conf
-c 读取指定配置文件
测试检查当前 nginx 使用的是哪个配置文件;检查配置文件的书写格式是否符合规范
nginx -t nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful配置文件
以分号结尾
以{}组织多条指令(参考 http)
include 引入
井号注释
$ 变量
/etc/nginx/nginx.confuser nginx;
worker_processes auto;
error_log /var/log/nginx/error.log notice;
pid /var/run/nginx.pid;
events {
# 最大连接数
worker_connections 1024;
}
http {
include /etc/nginx/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 /var/log/nginx/access.log main;
# 高效传输
sendfile on;
#tcp_nopush on;
# 长连接超时时间
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
}
/etc/nginx/conf.d/default.conf
server {
# 指定 80 端口
listen 80;
server_name localhost;
#access_log /var/log/nginx/host.access.log main;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
#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 /usr/share/nginx/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;
#}
}
/usr/share/nginx/html/index.html
访问 80 端口默认展示的页面
新建一个 test.html在 /usr/share/nginx/html/ 目录下新建一个 test.html,然后通过 ip:80/test.html 即可直接访问(或者 ip/test.html)
搭建一个静态文件的 nginx 服务在 /usr/share/nginx/ 目录下新建 test 文件夹
vi /etc/nginx/conf.d/default.conf
server {
# 指定 80 端口
listen 80;
server_name localhost;
#access_log /var/log/nginx/host.access.log main;
location / {
# *****************修改项*****************
# 将 html 修改为 test
root /usr/share/nginx/test;
index index.html index.htm;
}
#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 {
# *****************修改项*****************
# 将 html 修改为 test
root /usr/share/nginx/test;
}
# 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;
#}
}
在 /usr/share/nginx/test 目录下放一个 test.html,重启 nginx,之后即可通过 ip/test.html 访问



