近期经常看到有同事部署nginx时,不会nginx的基本设置。
下面共享了一个nginx.conf文件,并对一些常用的配置进行了简单的说明:(基本情况是同一台服务器上有tomcat提供的zcms或其他应用以及由nginx提供的站点静态服务两个,然后通过nginx的代理访问tomcat的应用。这样可以保证在仅开启80端口的情况下访问到后台和静态内容以及前端动态服务。)
1
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
|
#user nobody;
worker_processes 1;
#worker_processes 定义了nginx对外提供web服务时的worder进程数。
#最优值取决于许多因素,包括(但不限于)CPU核的数量、存储数据的硬盘数量及负载模式。
#不能确定的时候,将其设置为可用的CPU内核数将是一个好的开始(设置为“auto”将尝试自动检测它)。
#worker_rlimit_nofile 更改worker进程的最大打开文件数限制。如果没设置的话,这个值为操作系统的限制。
#设置后你的操作系统和Nginx可以处理比“ulimit -a”更多的文件,所以把这个值设高,这样nginx就不会有“too many open files”问题了。
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 51200;
#nginx最大连接数
}
http {
server_tokens off;
#server_tokens 并不会让nginx执行的速度更快,但它可以关闭在错误页面中的nginx版本数字,这样对于安全性是有好处的。
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 65;
#超时时间
client_max_body_size 2000m;
#提交html部分的数据大小,通常与上传文件的大小有关
gzip on;
gzip_types text/plain application/x-javascript text/css text/javascript application/x-httpd-php image/jpeg image/gif image/png;
#gzip支持及相关类型
ssi on;
#ssi支持
upstream cms{
server 220.181.152.146:8080;
#若本机IP可用localhost,有时127.0.0.1无效
}
server {
listen 80;
#监听的端口
# server_name xxxx;
#charset koi8-r;
#access_log logs/host.access.log main;
location /cms {
index index.html index.htm index.shtml index.zhtml;
proxy_pass http://cms;
proxy_redirect off;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
}
location / {
root /usr/tomcat/webapps/wwwroot_release/news;
#静态目录
index index.shtml index.html index.htm;
}
#error_page 403 /404.shtml;
error_page 404 /404.shtml;
#error_page 500 /500.shtml;
# redirect server error pages to the static page /50x.html
#
#error_page 500 502 503 504 /500.shtml;
#location = /500.shtml {
# root shtml;
#}
#错误页面及相关信息配置
}
#include /usr/local/nginx/vhosts/*;
#include只是一个在当前文件中包含另一个文件内容的指令。
}
|
通常新部署的应用需要http下的upstream、server、location /cms、location /等几个部分,可按照上述文件简单修改即可。
修改完成后可使用nginx -t 检查正确性,nginx -s reload重新加载配置并生效。