zoukankan      html  css  js  c++  java
  • nginx配置全过程

    事先声明,我用的是http://nginxcn.com/站BBS里面发布的NPMserver5.0,这个套件已经快有一年没更新了,所以不是最新的nginx,php,和mysql,但是也没落后多久。

    首先,在上述网址找到下载地址,以前有个安装过程,今天我找到的是直接解压缩,建议能“安装”的就安装,其实就是解压一下,但是这个过程中,它会把你的NPMserver的路径写到各个配置文件里面去,

    假如你下的是压缩版,那么需要到以下地方自行把NPMserver的安装目录改成你自己的

    /config.ini, /config.xml, /MySQL5.1/my.ini, /nginx/vhost.conf, php/php.ini

    比较麻烦是吧,有全文搜索工具的,可以直接在目录里面查找。这是准备工作,保证错误不要出在路径上,这可是低级的无意义错误。

    路径改好后,php.ini基本上不要做什么改动,除非你要装Xdebug给netbean用,那么就像我之前这篇文章一样进行设置,不谈。

    主要配置nginx,nginx的配置不熟的人是会非常头痛的,NPMserver有个好处,他把配置文件给分成了一个服务器配置文档(nginx.conf)和一个虚拟主机配置文档(vhost.conf),这样你分开设置,非常方便

    其中虚拟主机配置方面,这个软件还提供了ini,xml以及客户端的方式让你来填写,很不错,但是我没时间测那么多,直接是在配置文件里面写的,下面是一些需要注意的地方

    1,nginx.conf基本上你不要去动它,默认配置基本上可以跑很多网站了,你要去动的话,先了解清楚应该怎么改。

    2,下面这一段会是默认在vhost.conf里面的

    server {
        listen       80;
        server_name  nginx.a;
        location / {
            root   I:/NPMserv/www;
            index  index.html index.htm index.php default.php;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
        location ~ .*\.php?$ {
            root           D:/NPMserv/www;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  I:/NPMserv/www$fastcgi_script_name;
            include        fastcgi_params;
        }
    }
    

    上一篇文章我们给apache配置了虚拟路径,现在我们把这个虚拟路径也配到nginx里面去:

    server {
        listen       80;
        server_name  www.dxpie.com event.dxpie.com;
        location / {
            root   "I:/EasyPHP/www/pie";
            index  index.html index.htm index.php default.php;
    		if ($host ~ ^www\.dxpie\.com.*$) {
    			rewrite ^/?do-(.*)\.html?$ /index.php?act=public&todo=$1 last;
    		}
    		if ($host ~ ^event\.dxpie\.com.*$) {
    			rewrite ^/?([a-zA-Z_]+)(-(\d*))?-p(\d*)\.html?$ /event.php?mod=event&act=$1&id=$3&page=$4&todo=pager last;
    			rewrite ^/?([a-zA-Z_]+)(-(\d*))?-ap(\d*)\.html?$ /event.php?mod=event&act=$1&id=$3&page=$4&todo=ajaxpager last;
    			rewrite ^/?([a-zA-Z_]+)(-(\d*))?\.html?$ /event.php?mod=event&act=$1&id=$3 last;
    			rewrite ^/?(\w+)-(\d+)-c(\d)(\d+)\.html?$ event.php?mod=event&act=$1&pid=$2&gid=$4&index=$3&level=2 last;
    			rewrite ^/?(\w+)-(\d+)-c(\d)(\d+)-ap(\d+)\.html?$ event.php?mod=event&act=$1&pid=$2&gid=$4&index=$3&level=2&page=$5&todo=ajaxpager last;
    			break;
    		}
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
        location ~ .*\.php?$ {
            root           "I:/EasyPHP/www/pie";
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  "I:/EasyPHP/www/pie/$fastcgi_script_name";
            include        fastcgi_params;
        }
    }

    我用这个做例子,是因为可以解决很多问题

    2.1,每一个主机,就是一个server{}

    2.2,server_name, 可以同时配置多个域名,而不要像apache一样,一个域名要写个节点

    2.3,主机下面设server_name, 配置根目录,配置该路径的fastcig参数,自己看代码

    2.4,上面的URL重写演示了nginx的规则,对应的.htaccess列下下面,没时间学习的话,就从这两段看一下差别吧:

    RewriteCond %{HTTP_HOST} ^www\.dxpie\.com$
    RewriteRule ^do-(.*)\.html?$ index.php?act=public&todo=$1 [L]
    RewriteRule ^getfocus\.html?$ index.php?todo=focus [L]
    
    
    RewriteCond %{HTTP_HOST} ^event\.dxpie\.com$
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{HTTP_HOST} ^event\.dxpie\.com$
    RewriteRule ^([a-zA-Z_]+)(-(\d*))?\.html?$ event.php?mod=event&act=$1&id=$3 [QSA,L]
    RewriteCond %{HTTP_HOST} ^event\.dxpie\.com$
    RewriteRule ^([a-zA-Z_]+)(-(\d+))?-p(\d+)\.html?$ event.php?mod=event&act=$1&id=$3&page=$4&todo=pager [QSA,L]
    RewriteCond %{HTTP_HOST} ^event\.dxpie\.com$
    RewriteRule ^([a-zA-Z_]+)(-(\d*))?-ap(\d*)\.html?$ event.php?mod=event&act=$1&id=$3&page=$4&todo=ajaxpager [QSA,L]

    相比较而言,nginx的if语句更容易让大多数人接受吧

    2.5,最后一节的php配置,经过我的多次测试,光把默认的fastcgi_params给include进来是不够的,所以加了这一行:

    fastcgi_param SCRIPT_FILENAME “目录位置/$fastcgi_script_name”;照抄就是了,已经测试很多回了,愿意解释的可以留言:)

    3,经过上面的这个例子,配别的路径也就很称手了,没有url重写的话,会简洁很多,基本上只要加一下fastcgi_param这个就可以了,比如我把之前同easyphp一起装下来的phpmyadmin给虚拟进来,(我的apache,php,mysql用的是EASYPHP,相当强悍+方便,推荐在win上使用,请google之),那么我这么配置

    server {
        listen       80;
        server_name  my.sql;
        location / {
            root   "I:/EasyPHP/phpmyadmin";
            index  index.html index.htm index.php default.php;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
        location ~ .*\.php?$ {
            root           "I:/EasyPHP/phpmyadmin";
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  "I:/EasyPHP/phpmyadmin/$fastcgi_script_name";
            include        fastcgi_params;
        }
    }

    注意,my.sql是我在hosts文件里面指向127.0.0.1的,这样方便我以后直接用这个域名管理数据库

    4,当前这么访问到的数据库好像还是nginx的,既然我之前已经有了环境,那么肯定要把之前的用上,于是轮到/MySQL5.1/my.ini出场了,把里面的datadir改成你数据库的路径,basedir不要动:

    [mysqld]
    basedir=I:/NPMserv/MySQL5.1
    ;datadir=I:/NPMserv/MySQL5.1/data  这是原来的
    datadir="I:/EasyPHP/mysql/data"
    port=3306
    interactive_timeout=240
    wait_timeout=240

    现在启动npmserver试试, 访问localhost,是不是看到了经典的nginx界面?然后试试几个虚拟主机目录,phpmyadmin,是不是都成功了?不行的话仔细对着上面看一下。

    5,顺便分享一下python在nginx上我的配置

    #python
    server {
        listen       80;
        server_name  py.thon;
        location / {
                # host and port to fastcgi server
                fastcgi_pass 127.0.0.1:9000;
                fastcgi_param PATH_INFO $fastcgi_script_name;
                fastcgi_param REQUEST_METHOD $request_method;
                fastcgi_param QUERY_STRING $query_string;
                fastcgi_param CONTENT_TYPE $content_type;
                fastcgi_param CONTENT_LENGTH $content_length;
                fastcgi_pass_header Authorization;
                fastcgi_param REMOTE_ADDR           $remote_addr;
                fastcgi_param SERVER_PROTOCOL       $server_protocol;
                fastcgi_param SERVER_PORT           $server_port;
                fastcgi_param SERVER_NAME           $server_name;
                fastcgi_intercept_errors off;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
        
        location ^~ /media/ {
    		alias "I:/python26/Lib/site-packages/django/contrib/admin/media/";
        }
        location ^~ /site_media/{
    		alias "E:/codes/python/newtest/media/";
        }
        location ~* ^.+\.(jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js|mov)$ {
            root           "E:/codes/python/newtest";
            access_log off;
        }
    }
    你会发现还是有很大不同,当然pytoon加nginx和python加apache我会另外写文了。
  • 相关阅读:
    函数
    registry搭建及镜像管理-harbor
    用户输入和while 循环
    dockerfile
    字典,set
    if 语句
    alpine操作
    循环:列表,元组
    列表
    docker跨主机通信-overlay
  • 原文地址:https://www.cnblogs.com/walkerwang/p/1788195.html
Copyright © 2011-2022 走看看