nginx虚拟主机配置指南(超全超傻瓜式)

目录

一、虚拟主机配置实例(亲测可用)

1、单项目的(域名=test.single.com,项目名=single)

2、多项目的(域名=test.multi.com,项目名=multi)

二、Nginx相关参数详解

1、查看nginx配置文件位置

2、fastcgi.conf与fastcgi_param的区别

3、 PHP Primary script unknown 终极解决方法

4、location

5、try_files

6、rewrite

7、nginx配置中robots.txt文件作用


一、虚拟主机配置实例(亲测可用)

1、单项目的(域名=test.single.com,项目名=single)

server {
    listen       81;
    server_name  test.single.com;
    root         /Users/zhangsan/single/web;#该下面有index.php,且末尾不要/
    access_log   /data0/www/logs/single_access.log  main;#该项目nginx的一般日志
    error_log    /data0/www/logs/single_error.log;#该项目nginx的错误日志

    location /  {
        try_files $uri $uri/ /index.php?$query_string;#$args也行,效果等同于$query_string
    }

    location ~ \.php$ {
        include /usr/local/etc/nginx/fastcgi.conf;#各种fastcgi_param
        fastcgi_pass  127.0.0.1:9000;#默认fastcgi监听9000端口
        fastcgi_index  index.php;
    }

    location ~ /robots\.(txt|php) { #爬虫搜索去重
        include /usr/local/etc/nginx/fastcgi.conf;#各种fastcgi_param
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index  index.php;
    }

    location ~* "^/assets/(.*)" {
        root /data0/www/privdata/test.single.com/;
    }
}

2、多项目的(域名=test.multi.com,项目名=multi)

server {
    listen       82;
    server_name  test.multi.com;
    root         /Users/zhangsan/web/multi;#注意这里并没有直接到某个项目的web目录
    access_log   /data0/www/logs/multi_access.log  main;#该项目nginx的一般日志
    error_log    /data0/www/logs/multi_error.log;#该项目nginx的错误日志

    location / {
        root /Users/zhangsan/web/multi/frontend/web;
        index /frontend/web/index.php;
        try_files $uri $uri/ /frontend/web/index.php?$args;
    }

    location /admin {
        root /Users/zhangsan/web/multi/backend/web;
        try_files $uri /backend/web/index.php?$query_string;
        rewrite ^(/admin)/$ $1 permanent;
    }

    location ~ \.php$ {
        include /usr/local/etc/nginx/fastcgi.conf;#各种fastcgi_param
        fastcgi_pass  127.0.0.1:9000;#默认fastcgi监听9000端口
        fastcgi_index  index.php;
    }

    location ~ /robots\.(txt|php) { #爬虫搜索去重
        include /usr/local/etc/nginx/fastcgi.conf;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index  index.php;
    }

    location ~* "^/assets/(.*)" {
        root /data0/www/privdata/test.multi.com/;
    }
}

注意:多项目中的$uri

要么无$url/,如下:
location / {
    root /Users/zhangsan/web/multi/frontend/web;
    try_files $uri /frontend/web/index.php?$args;
}
要么有$url/,如下:
location / {
    root /Users/zhangsan/web/multi/frontend/web;
    index /frontend/web/index.php;
    try_files $uri $uri/ /frontend/web/index.php?$args;
}
所以显然,$uri/是由index来定义的,index默认值为(根目录下的)index.php,如果像这里更改了根目录,那么index也要跟着改变。

二、Nginx相关参数详解

1、查看nginx配置文件位置

方法1、systemctl status tengine.service   => /usr/local/matrix/etc/tengine.conf

● tengine.service - The Tengine HTTP and reverse proxy server
   Loaded: loaded (/usr/lib/systemd/system/tengine.service; enabled; vendor preset: disabled)
   Active: active (running) since 四 2019-05-16 10:13:31 CST; 8h ago
  Process: 974 ExecStart=/usr/local/matrix/sbin/tengine (code=exited, status=0/SUCCESS)
  Process: 934 ExecStartPre=/usr/local/matrix/sbin/tengine -t (code=exited, status=0/SUCCESS)
 Main PID: 990 (tengine)
   CGroup: /system.slice/tengine.service
           ├─990 nginx: master process /usr/local/matrix/sbin/tengine
           ├─996 nginx: worker process
           └─998 nginx: worker process
5月 16 10:13:30 VM systemd[1]: Starting The Tengine HTTP and reverse proxy server...
5月 16 10:13:31 VM tengine[934]: nginx: the configuration file /usr/local/matrix/etc/tengine.conf syntax is ok
5月 16 10:13:31 VM tengine[934]: nginx: configuration file /usr/local/matrix/etc/tengine.conf test is successful
5月 16 10:13:31 VM systemd[1]: PID 1369 read from file /usr/local/matrix/var/run/tengine.pid does not exist or is a zombie.
5月 16 10:13:31 VM systemd[1]: Started The Tengine HTTP and reverse proxy server.

方法2、brew info nginx  =>  /usr/local/etc/nginx/nginx.conf

….
The default port has been set in /usr/local/etc/nginx/nginx.conf to 8080 so that
nginx can run without sudo.
nginx will load all files in /usr/local/etc/nginx/servers/.

2、fastcgi.conf与fastcgi_param的区别

对比下 fastcgi.conf 与 fastcgi_params 文件,可以看出只有以下差异,即 fastcgi.conf 只比 fastcgi_params 多了一行:

fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;

原本只有 fastcgi_params 文件,fastcgi.conf 是 nginx 0.8.30 才引入的。主要为是解决以下问题:原本 Nginx 只有 fastcgi_params,后来发现很多人在定义 SCRIPT_FILENAME 时使用了硬编码的方式。于是为了规范用法便引入了 fastcgi.conf。不过这样的话就产生一个疑问:为什么一定要引入一个新的配置文件,而不是修改旧的配置文件?
这是因为 fastcgi_param 指令是数组型的,和普通指令相同的是:内层替换外层;和普通指令不同的是:当在同级多次使用的时候,是新增而不是替换。
换句话说,如果在同级定义两次 SCRIPT_FILENAME,那么它们都会被发送到后端,这可能会导致一些潜在的问题,为了避免此类情况,便引入了一个新的配置文件。
fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
include  fastcgi_params;

等价于:
include  fastcgi.conf;

3、 PHP Primary script unknown 终极解决方法

原因只有两个,一个是php-fpm找不到php文件,一个是php-fpm没有权限读取和执行文件

具体解决详见:https://segmentfault.com/a/1190000017789890

4、location

基本语法:location [=|~|~*|^~] /uri/ { … }

= 严格匹配。如果这个查询匹配,那么将停止搜索并立即处理此请求。

~ 为区分大小写匹配(可用正则表达式)

~* 为不区分大小写匹配(可用正则表达式)

!~为区分大小写不匹配

!~*为不区分大小写不匹配

^~ 如果把这个前缀用于一个常规字符串,那么告诉nginx 如果路径匹配那么不测试正则表达式。

”^”这个字符在中括号”[]”中被使用的话就表示字符类的否定,如果不是的话就是表示限定开头。

.* 就是单个字符匹配任意次,即贪婪匹配

.*? 是满足条件的情况只匹配一次,即最小匹配

5、try_files

语法解释:(先贴出官方的解释)

Checks the existence of files in the specified order and uses the first found file for request processing; the processing is performed in the current context. The path to a file is constructed from the fileparameter according to the root and alias directives. It is possible to check directory’s existence by specifying a slash at the end of a name, e.g. “$uri/”. If none of the files were found, an internal redirect to the uri specified in the last parameter is made. 

关键点1:按指定的file顺序查找存在的文件,并使用第一个找到的文件进行请求处理

关键点2:查找路径是按照给定的root或alias为根路径来查找的

关键点3:如果给出的file都没有匹配到,则重新请求最后一个参数给定的uri,就是新的location匹配

关键点4:如果是格式2,如果最后一个参数是 = 404 ,若给出的file都没有匹配到,则最后返回404的响应码

举例说明:

location /images/ {

    root /opt/html/;

    try_files $uri   $uri/  /images/default.gif; 

}

例1:请求 127.0.0.1/images/test.gif 会依次查找 

1.文件/opt/html/images/test.gif   

2.文件夹 /opt/html/images/test.gif/下的index文件  

3. 请求127.0.0.1/images/default.gif

4.其他注意事项

try-files 如果不写上 $uri/,当直接访问一个目录路径时,并不会去匹配目录下的索引页  即 访问127.0.0.1/images/ 不会去访问  127.0.0.1/images/index.html

其他用法:

location / {

    try_files /system/maintenance.html

              $uri $uri/index.html $uri.html

              @mongrel;

}

location @mongrel {

    proxy_pass http://mongrel;

}

以上中若未找到给定顺序的文件,则将会交给location @mongrel处理(相当于匹配到了@mongrel来匹配)

例2:匹配文件夹下的内容,如果没有转到/index.php

try_files /app/cache/ $uri /index.php;

index index.php index.html;

它将检测$document_root/app/cache/index.php,$document_root/app/cache/index.html 和 $document_root$uri是否存在

6、rewrite

1.多目录转成参数

abc.domian.com/sort/2 => abc.domian.com/index.php?act=sort&name=abc&id=2

if ($host ~* (.*)\.domain\.com) {

set $sub_name $1;   

rewrite ^/sort\/(\d+)\/?$ /index.php?act=sort&cid=$sub_name&id=$1 last;

2.目录对换

/123456/xxxx -> /xxxx?id=123456

rewrite ^/(\d+)/(.+)/ /$2?id=$1 last;

例如下面设定nginx在用户使用ie的使用重定向到/nginx-ie目录下:

if ($http_user_agent ~ MSIE) {

    rewrite ^(.*)$ /nginx-ie/$1 break;

}

3.目录自动加“/”

if (-d $request_filename){

    rewrite ^/(.*)([^/])$ http://$host/$1$2/ permanent;

}

7、nginx配置中robots.txt文件作用

1、屏蔽网站内的死链接。

2、屏蔽搜索引擎蜘蛛抓取站点内重复内容和页面。

3、阻止搜索引擎索引网站隐私性的内容。

猜你喜欢

转载自blog.csdn.net/wuhuagu_wuhuaguo/article/details/106269358
今日推荐