xshell 下载 http://jslin.cn/Xshell-7.0.0113p.exe xftp http://jslin.cn/Xftp-7.0.0111p.exe
优点
- 高并发好
- 反向代理 负载均匀
- 资源占用低
- 服务器有健康检查
- 代码配置简单
命令
- 查看 nginx ps -ef |grep nginx
- -t 检查
- 重启 nginx -s reopen
- 重新加载配置 nginx -s reload
发布目录 /usr/local/nginx/html
- 停止 nginx nginx -s quit
nginx 配置
autoindex 适合服务器下载文件列表读取 keepalive 超时时间
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;
# gzip_min_length 1k;
# gzip_comp_level 8;
server {
listen 5000;
server_name localhost;
gzip on;
gzip_disable "msie6";
gzip_vary on;
gzip_proxied any;
gzip_comp_level 6;
gzip_buffers 16 8k;
gzip_http_version 1.1;
gzip_min_length 256;
gzip_types application/atom+xml application/geo+json application/javascript application/x-javascript application/json application/ld+json application/manifest+json application/rdf+xml application/rss+xml application/xhtml+xml application/xml font/eot font/otf font/ttf image/svg+xml text/css text/javascript text/plain text/xml;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root dist;
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 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;
# }
#}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
反向代理和正向代理解释
正向代理: fan 樯可以理解为正向代理, 中间服务器代理了客户端去访问目标服务器, 隐藏了真正的客户端
反向代理: 用于负载均匀, 中间服务器代理了目标服务器和客户端打交道, 然后分发到其他服务器上面, 隐藏了真正的目标服务器
https://cloud.tencent.com/developer/article/1418457
nginx 配置详解
https://www.cnblogs.com/54chensongxia/p/12938929.html
nginx 安装和启动
安装时自行百度, 也可以通过该地址进行安装 https://www.cnblogs.com/itzgr/p/13266215.html
参数名称
$uri: 当前请求的URI,不带参数; $request_uri: 请求的 URI,带完整参数; $host: http请求报文中的host首部,如果没有则以处理此请求的虚拟主机的主机名代替; $hostname: Nginx 服务运行所在主机的主机名; $remote_addr: 客户端IP; $remote_prot: 客户端端口; $remote_user: 使用用户认证时客户端用户输入的用户名; $request_filename: 用户请求中的 URI 经过本地 root 或 alias 转换后映射的本地文件路径; $request_method: 请求方法,GET、POST、PUT; $server_addr: 服务器地址; $server_name: 服务器名称; $server_port: 服务器端口; $server_protocol: 服务器向客户端发送响应时的协议,如http/1.1、http/1.0; $scheme: 在请求中使用 scheme。如http://xxx.com中的http; $http_HEADER: 匹配请求报文中指定的HEADER; $http_host: 匹配请求报文中的 host 首部。
nginx 配置
主配置 nginx.conf
分为 全局 , event, http, server
全局配置
user nginx; #进程用户
worker_processes 1; #工作进程,配合和CPU个数保持一致
error_log /var/log/nginx/error.log warn; #错误日志路径及级别
pid /var/run/nginx.pid; #Nginx服务启动的pid
2
3
4
event 配置
events {
worker_connections 1024; #每个worker进程支持的最大连接数
use epoll; #内核模型,select、poll、epoll
}
2
3
4
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; #nginx不要缓存数据,而是一段一段发送
keepalive_timeout 65; #给客户端分配连接超时时间,服务器会在这个时间过后关闭连接。
#gzip on; #压缩
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
server 配置详解
server {
listen 80; #监听端口,默认80
server_name localhost; #提供服务的域名或主机名
#charset koi8-r;
#access_log logs/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 { #错误代码重定向到新的location
root html;
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server { #server段配置
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server { #server段配置
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key; #SSL证书配置
# 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;
# }
#}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
nginx 配置不同域名访问不同的地址
server {
server_name base.linuxds.com; # base.linuxds.com该域名访问下面base的目录
location / {
root /usr/share/nginx/base;
index index.html;
}
}
server {
server_name blog.linuxds.com;
location / {
root /usr/share/nginx/blog;
index index.html;
}
location /ok {
alias /usr/share/nginx/yes;
index index.html;
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
配置错误页面
server {
error_page 404 403 500 502 503 504 /baseerror.html; #配置错误页
location / {
root /usr/share/nginx/blog;
index index.html;
}
}
2
3
4
5
6
7
8
nginx 允许特定访问
root /usr/share/nginx/rewrite/;
allow 208.97.167.194;
allow 222.33.1.2;
allow 231.152.49.4;
deny all;
auth_basic "xhy";
auth_basic_user_file htpasswd;
2
3
4
5
6
7
nginx 配置下载
https://www.cnblogs.com/itzgr/p/13271580.html
[root@nginx01 ~]# vi /etc/nginx/conf.d/down.conf
server {
listen 80;
server_name download.linuxds.com;
location /down {
alias /download;
access_log /var/log/nginx/down.access.log main;
error_log /var/log/nginx/down.error.log warn;
autoindex on;
autoindex_localtime on;
autoindex_exact_size off;
charset utf-8,gbk;
if ($request_filename ~* ^.*?\.(txt|doc|pdf|rar|gz|zip|docx|exe|xlsx|ppt|pptx|conf)$)
{add_header Content-Disposition 'attachment;';}
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
杀掉 nginx pkill nginx 停止 nginx nginx -s stop 重启 nginx -s reload
nginx 配置下载站
location /download {
alias D:\\360Downloads;
sendfile on;
autoindex on; # 开启目录文件列表
autoindex_exact_size on; # 显示出文件的确切大小,单位是bytes
autoindex_localtime on; # 显示的文件时间为文件的服务器时间
# charset utf-8,gbk; # 避免中文乱码
}
2
3
4
5
6
7
8
压缩静态资源
xshell 下载 http://jslin.cn/Xshell-7.0.0113p.exe xftp http://jslin.cn/Xftp-7.0.0111p.exe nginx 版本
nginx plus
http://openresty.org/cn/
http://tengine.taobao.org/
解压 tar zxvf nginx.tar 默认进去的就是/root 文件目录 yum install -y gcc 安装 c 语言编译器 ./configure 可以检测安装问题 配置安装路径 ./configure --prefix=/usr/local/nginx yum install -y pcre pcre-devel 安装 pcre yum install -y zlib zlib-devel make make install 完成安装 nginx 关防火墙 systemctl stop firewalld.service 禁止防火墙开机启动 systemctl disable firewalld.service 可以访问拉 启动 nginx ./nginx 启动 ./nginx -s stop 快速停止 ./nginx -s quit 已经开始的任务完成后停止, 不再创建心得链接 ./nginx -s reload 重新加载配置 nginx 安装成脚本 vi /usr/lib/systemd/system/nginx.service 粘贴那堆命令, 自启动脚本 重新加载系统服务 systemctl daemon-reload 查看 nginx 的状况 ps -ef | grep nginx 关闭服务 设置开启启动 systemctl enable nginx.service 重启 reboot 绘制原型的在线工具 https://www.processon.com/diagrams
nginx 配置 includes 引入配置文件, 通过 mime.type 来告诉浏览器怎么打开该文件 sendfile
server_name 可以配置多个, 还可以使用通配符
反向代理 nginx 和应用服务器在一起, 用户通过 nginx 来访问应用服务器, nginx 代理了用户请求, 由服务器这边反向提供的代理服务器
正向代理 用户通过可以访问外网的服务器, 用户和代理服务器是一起的, 主动配置的, 比如 vpn 网关也是代理服务器
隧道模式 进出都是一个地方 DR 模型 进时会通过 nginx, 返回给客户端时不需要通过 nginx
负载均衡
proxy_pass http://www.jslin.cn; 代理到这个地址
重启 nginx systemctl reload nginx 配置负载均衡 upstream httpds{
server 192.168.44.102:80;
server 192.168.44.103:80;
} server: { location / {
proxy_pass
}
vue history nginx 配置
https://www.hangge.com/blog/cache/detail_3140.html
一级目录配置
server {
listen 9090;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root /Volumes/BOOTCAMP/Vue/hangge-app/dist;
try_files $uri $uri/ /index.html;
index index.html;
add_header Access-Control-Allow-Origin *;
}
#error_page 404 /404.html;
<!-- vue配置 -->
const router = new VueRouter({
mode: 'history',
base: process.env.BASE_URL,
routes
})
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
二级目录
server {
listen 9090;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location /hangge {
alias /Volumes/BOOTCAMP/Vue/hangge-app/dist;
try_files $uri $uri/ /hangge/index.html;
index index.html;
add_header Access-Control-Allow-Origin *;
}
#error_page 404 /404.html;
<!-- vue中配置 -->
const router = new VueRouter({
mode: 'history',
base: '/hangge',
routes
})
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
nginx 学习网站
https://juejin.im/post/6844904144235413512?utm_source=gold_browser_extension#heading-21