Nginx基本使用
Nginx是一个高性能的HTTP和反向代理服务器,占用资源少,并发能力强。
1、安装与启动
- yum install nginx
- 查看安装目录 whereis nginx
-
- /usr/sbin/nginx nginx启动目录
- /etc/nginx 配置目录
- /usr/lib64/nginx 模块目录
- /usr/share/nginx nginx的缓存目录
- 启动命令 进入/usr/sbin 启动命令 ./nginx
- 关闭命令 ./nginx -s stop 或者 ./nginx -s quit
- 修改配置重新启动 ./nginx -s reload
- 以指定配置文件启动 nginx -c /xxx/xxx/nginx.conf
windows 解压即可
2、相关配置说明
5 user nginx; #运行nginx的用户
6 worker_processes auto; #工作进程数量,建议不要修改,保持自动
7 error_log /var/log/nginx/error.log; #错误日志文件的输出位置和输出级别
8 pid /run/nginx.pid; #pid文件位置
9
10 # Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
11 include /usr/share/nginx/modules/*.conf;
12
13 events {
14 worker_connections 1024; #每个进程的最大连接数,默认1024
15 }
16
17 http { #HTTP配置
18 log_format main '$remote_addr - $remote_user [$time_local] "$request" '
19 '$status $body_bytes_sent "$http_refer er" '
20 '"$http_user_agent" "$http_x_forwarded_for"'; #日志的格式
22 access_log /var/log/nginx/access.log main; #访问日志文件的位置
23
24 sendfile on; #是否调用sendfile函数来输出文件
25 tcp_nopush on;
26 tcp_nodelay on; #是否启用nodelay算法
27 keepalive_timeout 65; #连接超时时间
28 types_hash_max_size 2048;
29
30 include /etc/nginx/mime.types; #支持的文件类型
31 default_type application/octet-stream; #默认文件类型
32
33 # Load modular configuration files from the /etc/nginx/conf.d directory.# 从/etc/nginx/conf.d目录加载模块化配置文件
34 # See http://nginx.org/en/docs/ngx_core_module.html#include
35 # for more information.
36 include /etc/nginx/conf.d/*.conf; #引入外部配置文件,包含虚拟主机配置
37
#以下为虚拟主机配置
38 server {
39 listen 80 default_server; #监听端口 80
40 listen [::]:80 default_server;
41 server_name _; #主机域名
42 root /usr/share/nginx/html; # 默认页面路径
43
44 # Load configuration files for the default server block.
45 include /etc/nginx/default.d/*.conf;
46
47 location / {
48 }
49
#404页面
50 error_page 404 /404.html;
51 location = /40x.html {
52 }
#错误的反馈页面
54 error_page 500 502 503 504 /50x.html;
55 location = /50x.html {
56 }
57 }
58
#服务器的htttps设置
59 # Settings for a TLS enabled server.
# 启用TLS的服务器的设置
60 #
61 # server {
62 # listen 443 ssl http2 default_server;
63 # listen [::]:443 ssl http2 default_server;
64 # server_name _;
65 # root /usr/share/nginx/html;
66 #
67 # ssl_certificate "/etc/pki/nginx/server.crt"; #ssl证书路径
68 # ssl_certificate_key "/etc/pki/nginx/private/server.key"; #ssl证书key
69 # ssl_session_cache shared:SSL:1m;
70 # ssl_session_timeout 10m;
71 # ssl_ciphers HIGH:!aNULL:!MD5;
72 # ssl_prefer_server_ciphers on;
73 #
74 # # Load configuration files for the default server block. #加载默认服务器块的配置文件
75 # include /etc/nginx/default.d/*.conf;
76 #
# 以下为错误页面配置
77 # location / {
78 # }
79 #
80 # error_page 404 /404.html;
81 # location = /40x.html {
82 # }
83 #
84 # error_page 500 502 503 504 /50x.html;
85 # location = /50x.html {
86 # }
87 # }
88
89 }
90
3、示例代码
server {
listen 80;
server_name localhost;
client_max_body_size 1024M;
location / {
proxy_pass http://localhost:8080;
proxy_set_header Host $host:$server_port;
}
}
#负载均衡代码 轮询
upstream test {
server localhost:8080;
server localhost:8081;
}
#权重
upstream test {
server localhost:8080 weight=9;
server localhost:8081 weight=1;
}
#iphash
upstream test {
ip_hash;
server localhost:8080;
server localhost:8081;
}
server {
listen 81;
server_name localhost;
client_max_body_size 1024M;
location / {
proxy_pass http://test;
proxy_set_header Host $host:$server_port;
}
}
server {
listen 80;
server_name localhost;
location / {
root e:/wwwroot;
index index.html;
}
# 所有静态请求都由nginx处理,存放目录为html
location ~ .(gif|jpg|jpeg|png|bmp|swf|css|js)$ {
root e:/wwwroot;
}
# 所有动态请求都转发给tomcat处理
location ~ .(jsp|do)$ {
proxy_pass http://test;
}
#错误页面
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root e:/wwwroot;
}
}
|