centos下nginx虚拟机配置 需要注意的地方

本文详细介绍了Nginx服务器的配置方法,包括日志格式设置、监听端口配置、URL重写规则、FastCGI连接配置及缓存策略等关键内容。
  1 #设定日志格式
  2 log_format  auto  '$remote_addr - $remote_user [$time_local] "$request" '
  3 '$status $body_bytes_sent "$http_referer" '
  4 '"$http_user_agent" $http_x_forwarded_for';
  5 server {
  6      listen 80;            #换成你的IP地址
  7      client_max_body_size 100M;
  8      server_name  www.auto.com;  #换成你的域名
  9      charset utf-8;
 10      index index.html index.htm index.php;
 11      root   /home/wwwroot/auto;         #你的站点路径
 12
 13
 14      #打开目录浏览,这样当没有找到index文件,就也已浏览目录中的文件
 15      autoindex on;
 16
 17      #if (-d $request_filename) {
 18      #rewrite ^/(.*)([^/])$ http://$host/$1$2/ permanent;
 19      #}
 20
 21      error_page  404              /404.html;
 22      location = /40x.html {
 23      root  /home/wwwroot/auto;       #你的站点路径
 24      charset   on;
 25      }
 26
 27      # redirect server error pages to the static page /50x.html
 28      #
 29      error_page   500 502 503 504  /50x.html;
 30      location = /50x.html {
 31      root   /home/wwwroot/auto;      #你的站点路径
 32      charset   on;
 33      }
 34
 35     location / {
 36         index  index.php;
 37         #rewrite规则如下:
 38         rewrite ^/$ /index.php last;
 39         rewrite ^/(?!index\.php|robots\.txt|static)(.*)$ /index.php/$1 last;
 40     }
 41
 42      #将客户端的请求转交给
 43      location ~ ^(.+\.php)(.*)$ {
 44              # root           /home/wwwroot/auto/source;
 45              fastcgi_pass   127.0.0.1:9000;
 46              fastcgi_index  index.php;
 47              fastcgi_split_path_info ^(.+\.php)(.*)$;
 48              fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
 49              #fastcgi_param  PATH_INFO $fastcgi_path_info;
 50              fastcgi_param        PATH_TRANSLATED        $document_root$fastcgi_path_info;
 51              include        fastcgi_params;
 50      }
 51
 52
 53          #网站的图片较多,更改较少,将它们在浏览器本地缓存15天
 54      location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
 55      {
 56      expires      15d;
 57      }
 58
 59      #网站会加载很多JS、CSS,将它们在浏览器本地缓存1天
 60      location ~ .*\.(js|css)?$
 61      {
 62      expires      1d;
 63      }
 64
 65      location /(WEB-INF)/ {
 66      deny all;
 67      }
 68
 69      #设定本虚拟主机的访问日志
 70      access_log  /usr/local/nginx/logs/down/auto.log  auto;   #日志的路径,每个虚拟机一个,不能相同
 71      server_name_in_redirect  off;

 72 }

 

1、首先在nginx配置文件nginx.conf同级目录下建立vhost文件夹,vhost中可以建立你所需要的虚拟主机的配置文件,如:www.test1.com.conf,www.test2.com.conf

2、虚拟主机配置文件代码如上文中的代码所示,需要注意的地方是

 35     location / {
 36         index  index.php;
 37         #rewrite规则如下:
 38         rewrite ^/$ /index.php last;
 39         rewrite ^/(?!index\.php|robots\.txt|static)(.*)$ /index.php/$1 last;
 40     }

此处为url的重写规则,该文件项目为CI框架时所配置的。

3、第二个需要注意的地方是nginx如何将客户端的请求转交给php的fast-cgi,此处一定要注意location后面的正则,代表了以.php文件结束的,并且php文件后可以跟参数或者其他uri段的。

  location ~ ^(.+\.php)(.*)$ {
/*******/
 }

转载于:https://my.oschina.net/yueguanqun/blog/65125

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值