nginx 网站的建立及使用过程中遇到的问题

1.单一网站

1)使用默认80端口访问:

server {

        listen      80;              #端口监听

        location / {

            root  /home/ceshi; #网站地址

            index  index.html index.htm index.php;  #默认访问页面

        }

        error_page  500 502 503 504  /50x.html;  #错误页面

        location = /50x.html {

            root  html;

        }

        access_log  logs/ceshi-access.log  main;  #访问日志

        error_log  logs/ceshi-error.log  notice;    #错误日志

    }

2) 域名访问或者是ip+端口访问

server {

        listen      8001;       #端口监听

        server_name  ceshi.loc;     #域名配置    

        location / {

            root  /home/ceshi;            #网站地址

            index  index.html index.htm index.php;        #定义首页索引文件的名称

        }

        error_page  500 502 503 504  /50x.html;        #错误页面

        location = /50x.html {

            root  html;

        }

        access_log  logs/ceshi-access.log  main;        #访问日志

        error_log  logs/ceshi-error.log  notice;              #错误日志

    }

2.后端为index.php的网站

server {

        listen      8002;

        root  /home/ceshi;            #将root变成server的全局变量

        location / {

            index  index.html index.htm index.php;    #默认访问页面

        }

        location ~ \.php$ {

            fastcgi_pass  127.0.0.1:9000;     #php-fpm监听的地址

            #fastcgi_index  index.php;

            #fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;

            #include        fastcgi_params;

            include        fastcgi.conf;   #等价于上面两条代码

        }

        error_page  500 502 503 504  /50x.html;

        location = /50x.html {

            root  html;

        }

        access_log  logs/ceshi-access.log  main;      #访问日志

        error_log  logs/ceshi-error.log  notice;         #错误日志

    }

3.php-ci 框架网站的搭建

nginx 网站的建立及使用过程中遇到的问题_第1张图片
CI框架结构图

server {

        listen      8003;

        root    /home/advert/;

        index  index.htm index.php ;

        location / {

            fastcgi_pass  127.0.0.1:9000;

            fastcgi_param SCRIPT_FILENAME $document_root/index.php;

            fastcgi_param PATH_INFO $fastcgi_path_info;

            ##正则解析路径     

            fastcgi_split_path_info ^(.+\.php)(.*)$;

            #fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;                #include        fastcgi_params;           

            include        fastcgi.conf;   #等价于上面两条代码

        }

        error_log  logs/advert-error.log  notice;

        access_log  logs/advert-access.log  main;

    }

4.反向代理

server {

        listen      8004;

        location /{

                proxy_pass  http://192.168.9.221:8001;         #反向代理的网址

        }

        error_log  logs/pass-error.log  notice;

        access_log  logs/pass-access.log  main;

    }

5.负载均衡

upstream ceshi {          #负载均衡的地址

            server 192.168.9.221:8001;

            server 192.168.9.221:8002;

   server 192.168.9.221:8002 weight=1 max_fails=1 down;

          #weight权重    max_fail最大失败次数     down 不参与负均衡   fail_timeout 失败超时时间  Backup备份

        }

    server {

        listen      8005;

        location / {

              #设置主机头和客户端真实地址,以便服务器获取客户端真实IP

                proxy_set_header Host $host;

                proxy_set_header X-Real-IP $remote_addr;

                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

                proxy_buffering off;                  #禁用缓存

                proxy_pass http://ceshi;     #负载均衡的名称

        }

        error_page  500 502 503 504  /50x.html;

        location = /50x.html {

            root  html;

        }

    error_log  logs/load-error.log  notice;       

 access_log  logs/load-access.log  main;

    }

补充知识点:

A.nginx 的 upstream目前支持分配方式

1)、轮询(默认) 

每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除。

 2)、weight 

指定轮询几率,weight和访问比率成正比,用于后端服务器性能不均的情况。

 3)、ip_hash 

每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session的问题。  

4)、fair(第三方) 

按后端服务器的响应时间来分配请求,响应时间短的优先分配。

5)、url_hash(第三方)

B.每个设备的状态设置汇总:

1).down 表示单前的server暂时不参与负载 

2).weight 默认为1.weight越大,负载的权重就越大。 

3).max_fails:允许请求失败的次数默认为1.当超过最大次数时,返回proxy_next_upstream模块定义的错误 

4).fail_timeout: max_fails次失败后,暂停的时间。 

5).backup: 其它所有的非backup机器down或者忙的时候,请求backup机器。所以这台机器压力会最轻

6.单独提出配置文件

在nginx.conf文件中

include /usr/local/nginx/conf/service.conf;

server.conf中指定server模块的定义

问题汇总:

1)[emerg] unknown log format "main" in /usr/local/nginx/conf/nginx.conf:XX

解决方法:

打开nginx.conf,"main"错误是因为丢失了log_format选项,默认是被屏蔽掉了,将屏蔽打开就好了

2)Nginx配置文件问题导致打不开网站unknown directive

解决方法:

注意符号两面要有空格

3)[error] 1723#0: *10 connect() failed (111: Connection refused) while connecting to upstream, client: 192.168.9.11, server: , request: "GET / HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "192.168.9.11:8002"

造成原因: 9000端口没有监听,php-fpm没有启动

解决方法:

A。ubuntu:   apt-get install php5-fpm     

  service   php5-fpm start

B. 源码安装

在安装php 的时候需要指定参数--enable-fpm(php未安装的时候)

4.FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream

造成的原因:定义调用脚本文件路径有问题

解决方法:

fastcgi_param SCRIPT_FILENAME  项目路径$fastcgi_script_name;

 include        fastcgi_params;

5) FastCGI sent in stderr: "Access to the script '/home/advert' has been denied (see security.limit_extensions)"

解决方法:

a.修改php.ini配置文件中cgi.fix_pathinfo=1

b.FPM的security.limit_extension设置用于限制允许解析的主脚本的扩展名。它可以防止恶意代码被执行。默认值是简单的.php可以配置/etc/php5/fpm/pool.d/www.conf

将/etc/php5/fpm/pool.d/www.conf文件中的security.limit_extension注释打开

c.重新启动php-fpm和nginx服务,页面能够正常访问

你可能感兴趣的:(nginx 网站的建立及使用过程中遇到的问题)