rss· 投稿· 设为首页· 加入收藏· 繁體版
当前位置: 火魔网 » 应用技术 » Web服务器

NGINX配置

昨天晚上突然心血来潮想要自己配置一下Nginx玩玩,但配置过程中发现了许多问题,但经过仔细测试之后问题都迎刃而解,在这里我将心得写下供网友参考:

Nginx.conf文件

1 #启动进程
2 worker_processes 1;
3 #全局错误日志及PID文件
4 error_log   /opt/nginx/logs/error.log   crit;
5
6 pid         /opt/nginx/logs/nginx.pid;
7 #工作模式及连接数上限
8 events {
9      worker_connections  64;
10 }
11 #设定http服务器,利用它的反向代理功能提供负载均衡支持
12 http {
13      #设定mime类型
14      include        /opt/nginx/conf/mime.types;
15      default_type   application/octet-stream;
16      #charset   gb2312;
17      server_names_hash_bucket_size 128;
18      #设定请求缓冲
19      client_header_buffer_size 32k;
20      large_client_header_buffers 4 32k;
21      
22      keepalive_timeout 60;
23
24      fastcgi_connect_timeout 300;
25      fastcgi_send_timeout 300;
26      fastcgi_read_timeout 300;
27      fastcgi_buffer_size 64k;
28      fastcgi_buffers 4 64k;
29      fastcgi_busy_buffers_size 128k;
30      fastcgi_temp_file_write_size 128k;
31      client_body_temp_path /opt/nginx/temp/client_body_temp;
32      proxy_temp_path /opt/nginx/temp/proxy_temp;
33      fastcgi_temp_path /opt/nginx/temp/fastcgi_temp;
34      #开启gzip模块
35      gzip on;
36      gzip_min_length   1k;
37      gzip_buffers     4 16k;
38      gzip_http_version 1.0;
39      gzip_comp_level 2;
40      gzip_types        text/plain application/x-javascript text/css application/xml;
41      gzip_vary on;
42     
43      client_header_timeout   3m;
44      client_body_timeout     3m;
45      send_timeout           3m;
46      sendfile                 on;
47      tcp_nopush               on;
48      tcp_nodelay             on;
49      #设定虚拟主机
50      include        /opt/nginx/conf/vhost.conf;
51 }

vhost.conf文件:

1 server {
2          listen       80;
3          client_max_body_size 10M;
4          server_name   localhost;
5          charset utf-8;
6          index index.html index.htm index.php;
7          #设置D:\wwwroot 为documentroot
8          #root    /cygdrive/d/wwwroot;
9          root    /opt/www;
10         
11          #打开目录浏览,这样当没有找到index文件,就也已浏览目录中的文件
12          autoindex on;
13         
14          if (-d $request_filename) {
15              rewrite ^/(.*)([^/])$ http://$host/$1$2/ permanent;
16          }
17         
18          error_page  404               /404.html;
19          location = /40x.html {
20              root   /opt/www;
21              charset    on;
22          }
23
24          # redirect server error pages to the static page /50x.html
25          #
26          error_page   500 502 503 504   /50x.html;
27          location = /50x.html {
28              root    /opt/www;
29              charset    on;
30          }
31         
32          #将客户端的请求转交给fastcgi
33          location ~ .*\.(php|php5)?$ {
34              fastcgi_pass   127.0.0.1:9000;
35              include /opt/nginx/conf/fastcgi_params;
36          }
37         
38          #网站的图片较多,更改较少,将它们在浏览器本地缓存15天
39          location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
40          {
41            expires       15d;
42          }
43         
44          #网站会加载很多JS、CSS,将它们在浏览器本地缓存1天
45          location ~ .*\.(js|css)?$
46          {
47            expires       1d;
48          }
49         
50          location /(WEB-INF)/ {
51              deny all;
52          }
53         
54          #设定日志格式
55          log_format   access   '$remote_addr - $remote_user [$time_local] "$request" '
56               '$status $body_bytes_sent "$http_referer" '
57               '"$http_user_agent" $http_x_forwarded_for';
58          #设定本虚拟主机的访问日志
59          access_log   /opt/nginx/logs/access.log   access;
60 }

在配置的过程中我发现一个问题,那就是nginx本是linux下的产物,如果要放在win下,应该如何写路径呢,经过仔细思考和测试,发现可以将路径写为/cygdrive/d/wwwroot,因为win版的nginx是用cygwin编译的,得写上win下模拟linux下的路径,另外,我的配置文件对ngnix进行了一定程序上的优化,注解很多,如果大家有什么问题,谢联系我,谢谢!・

顶一下
(0)
踩一下
(0)