nginx的特点
- 轻量级
- 并发能力强
- 高度模块化
- 负载均衡
- 反向代理
正向代理和反向代理
https://www.cnblogs.com/taostaryu/p/10547132.html
正向代理
- 正向代理类似一个跳板机, 代理访问外部资源.
- 比如我们在国内访问不到谷歌, 可以通过正向代理访问
- 隐藏了真实的请求客户端,服务端不知道真实的客户端是谁
用途
- 访问原来无法访问的资源
- 缓存, 加速访问资源
- 对客户端访问进行认证
- 隐藏了用户的资料
反向代理
- 反向代理的运行方式 : 以代理服务器来接收internet上的连接请求, 然后将请求转发给内部网络上的服务器, 并将从服务器上得到的结果返回给internet上请求连接的客户端.
作用
- 负载均衡.
安装nginx
- 包管理工具安装
- 安装先决条件 : sudo yum install yum-utils
- 创建目录 : touch /etc/yum.repos.d/nginx.repo
- 把以下代码放到文件里
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
- 使用nginx主线包 : sudo yum-config-manager --enable nginx-mainline
- 安装nginx : sudo yum install nginx
- 当提示接受GPG密钥时,验证指纹是否与573B FD6B 3D8F BC64 1079 A6AB ABF5 BD82 7BD9 BF62匹配,如果匹配则接受。
启动nginx
- 启动 : systemctl start nginx
- 查看进程 : ps -aux | group nginx
查看配置文件
user nginx; ## 用户叫nginx
worker_processes 1; ## worker进程一个
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
# 标识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;
#charset koi8-r;
#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;
}
nginx命令
- 启动nginx : systemctl start nginx
- 关闭nginx服务 : systemctl stop nginx
- 设置开机自启 : systemctl enable nginx
- 查看任务 : ps -aux | grep nginx
部署静态页面
- 创建 nginx.conf文件
user root;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
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;
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;
location / {
root /root/myproject/2048;
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;
}
}
}
- 启动 : nginx -c 默认配置目录
- 这里是nginx -c /root/myproject/2048/nginx.conf
- 关闭 : nginx -s signal stop
- 重启 : nginx -s signal reload