【nginx】nginx 配置那些事儿

nginx 是一款具备高负载能力的 web 服务器,也是 LNMP 架构的主要角色之一。如今愈来愈多的开发者选择 nginx 做为 php 的好搭档,替代 apache 的位置。下面我以 Mac 系统为例,介绍下 nginx 的配置php

基本配置

打开 nginx.conf,找到 http 下的 server,前几行的内容分别是:html

listen  8080;                #监听端口
server_name  localhost;      #本地域名
root  /usr/local/var/www;    #项目根目录

nginx 默认监听8080端口,你能够改为 80 端口。默认项目根目录也能够更改。不过更改以后要从新载入配置文件才能生效:node

sudo nginx -s reload

注意:若是你不喜欢localhost,你想要一个个性的本地域名,好比www.test.com,首先要编辑 hosts 文件:nginx

sudo vim /etc/hosts

添加一条:web

127.0.0.1     www.test.com

而后修改 nginx.conf:apache

server_name    www.test.com

总之 nginx.conf 中设置的域名,必须在 hosts 文件中存在!vim


隐藏入口文件

在咱们开发项目的时候,通常会有隐藏入口文件的需求。依然是在 http 下的 server,咱们找到location /,在大括号内作修改。切记ci框架和tp框架的写法稍有不一样,具体以下:segmentfault

location / {
       index  index.php index.html index.htm;
       if (!-e $request_filename) {
            rewrite ^/(.*)$ /index.php?$1 last;    #ci框架写法
            #rewrite ^/(.*)$ /index.php?s=/$1 last;    #tp框架写法
            break;
       }
}

若是你用的是tp5, 入口文件在 public 目录下,可是你不想在URL中写localhost/public/访问入口文件,你想直接经过localhost/访问,你能够这样写:服务器

rewrite ^/(.*)$ /public/index.php?s=/$1 last;

其实隐藏入口文件就是 nginx 作了下路由,看懂它的正则,其实不难理解。架构


解析php

若是要 nginx 正常解析 php,首先要在安装好 php 以后,启动 php-fpm。启动方法:

sudo php-fpm -D

上一步完成以后,接下来仍是要修改 nginx.conf。server 下找到location ~ \.php$这一行,包括它后面{}中的内容,去掉注释,也就是去掉前面的#号,修改为这样:

location ~ \.php(\/.*)*$ {
      fastcgi_pass   127.0.0.1:9000;
      fastcgi_index  index.php;
      fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;

      fastcgi_split_path_info  ^((?U).+\.php)(/?.+)$;
      fastcgi_param  PATH_INFO  $fastcgi_path_info;
      fastcgi_param  PATH_TRANSLATED  $document_root$fastcgi_path_info;
      include        fastcgi_params;
}

如图:

clipboard.png

从新载入配置文件。此时 nginx 就能够配合 php-fpm 正确解析 PHP 了。


多站点设置

前面咱们修改配置文件的代码位置,都是在 http 下的 server 里。其实一个 server 就至关于一个站点。nginx.conf 下 http 模块默认开启一个 server,就是只有一个站点。若是咱们要多站点,就须要添加多个 server。

如今咱们要添加一个站点,假设这个站点的域名是 www.test2.com, 可是 server 不写在 nginx.conf 里。nginx 为咱们提供了存放多站点配置文件的目录,咱们切换到这个目录:

cd /usr/local/etc/nginx/servers/

而后新建配置文件:

vim www.test2.com.conf

里边写一个 server:

server {

    listen  80;
    server_name  www.test2.com;

    index  index.html index.php;

    root  /usr/local/var/test2;

    location / {
         index  index.php index.html;
         rewrite ^/(.*)$ /public/index.php?s=/$1 last;
         break;
    }

    location ~ \.php$ {
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;

        fastcgi_split_path_info  ^((?U).+\.php)(/?.+)$;
        fastcgi_param  PATH_INFO  $fastcgi_path_info;
        fastcgi_param  PATH_TRANSLATED  $document_root$fastcgi_path_info;
        include        fastcgi_params;
    }
    
}

保存退出,从新载入配置文件。最后在 hosts 中添加:

127.0.0.1   www.test2.com

此时,www.test2.com 就能够访问到你的新项目目录 /usr/local/var/test2下了!


反向代理设置

个人应用场景是这样的。假设个人服务器上存放着一个 node 项目,node 占用着80端口,能够正常运行。可是我想再放一个 php 项目,此时这个php项目就不能是80端口了。若是想 node 项目和 php 项目均适用80端口,则须要利用 nginx 作反向代理设置。

解决方案是,node 项目设置成非 80 端口,好比3000,nginx 添加一个站点,假设是www.test3.com,访问 80 端口,代理到 3000 端口便可。

设置方法如上一节添加新站点:

cd /usr/local/etc/nginx/servers/

而后新建配置文件:

vim www.test3.com.conf

写入一个 server:

server {
    listen 80;
    server_name www.test3.com;
    location / {
        proxy_pass http://127.0.0.1:3000;
    }
}

访问 www.test3.com,就能够访问到 node 项目了!


本文由 杨成功 原创,更多原创内容请到专栏 杨成功的全栈之路

相关文章
相关标签/搜索