您正在查看: 标签 nginx 下的文章

Yii的urlManager使用

因为yii的连接并不优雅如,http://hanc.cc/index.php?r=site/index

yii中的 urlManager 组件可以使连接更加优雅

在config下的web.php component加上

'urlManager' => [
        'class' => 'yii\web\UrlManager',
        // Disable index.php
        'showScriptName' => false,
        // Disable r= routes
        'enablePrettyUrl' => true,
        'rules' => array(
            '/' => 'my-module/password/index',
            '/user' => 'my-module/user/index',
            '<controller:\w+>/<id:\d+>' => '<controller>/view',
            '<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
            '<controller:\w+>/<action:\w+>' => '<controller>/<action>',
        ),
  ],

nginx的配置如下

server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;

root /usr/share/nginx/html;
index index.php index.html index.htm;

# Make site accessible from http://localhost/
server_name localhost MY-IP;

location / {
    # First attempt to serve request as file, then
    # as directory, then fall back to displaying a 404.
    # try_files $uri $uri/ =404;
    # Uncomment to enable naxsi on this location
    # include /etc/nginx/naxsi.rules

    try_files $uri $uri/ /index.php?$args;
}

location @rewrite{
          rewrite ^/(.*)$ /index.php?$args;
     }

error_page 404 /404.html;
    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
        root /usr/share/nginx/html;
    }

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }                                                                                              }

因为yii的连接并不优雅如,http://hanc.cc/index.php?r=site/index

yii中的 urlManager 组件可以使连接更加优雅

在config下的web.php component加上

'urlManager' => [
        'class' => 'yii\web\UrlManager',
        // Disable index.php
        'showScriptName' => false,
        // Disable r= routes
        'enablePrettyUrl' => true,
        'rules' => array(
            '/' => 'my-module/password/index',
            '/user' => 'my-module/user/index',
            '<controller:\w+>/<id:\d+>' => '<controller>/view',
            '<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
            '<controller:\w+>/<action:\w+>' => '<controller>/<action>',
        ),
  ],

nginx的配置如下

server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;

root /usr/share/nginx/html;
index index.php index.html index.htm;

# Make site accessible from http://localhost/
server_name localhost MY-IP;

location / {
    # First attempt to serve request as file, then
    # as directory, then fall back to displaying a 404.
    # try_files $uri $uri/ =404;
    # Uncomment to enable naxsi on this location
    # include /etc/nginx/naxsi.rules

    try_files $uri $uri/ /index.php?$args;
}

location @rewrite{
          rewrite ^/(.*)$ /index.php?$args;
     }

error_page 404 /404.html;
    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
        root /usr/share/nginx/html;
    }

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }                                                                                              }

Nginx配置详解

语法规则

location [=|~|~*|^~] /uri/ { … }

= 开头表示精确匹配

^~ 开头表示uri以某个常规字符串开头,理解为匹配 url路径即可。nginx不对url做编码,因此请求为/static/20%/aa,可以被规则^~ /static/ /aa匹配到(注意是空格)。

~ 开头表示区分大小写的正则匹配

~* 开头表示不区分大小写的正则匹配

/ 通用匹配,任何请求都会匹配到

多个location配置的情况下匹配顺序为(参考资料而来,还未实际验证,试试就知道了,不必拘泥,仅供参考):

首先匹配 =,其次匹配^~, 其次是按文件中顺序的正则匹配,最后是交给 /通用匹配。当有匹配成功时候,停止匹配,按当前匹配规则处理请求。

location例子

例子,有如下匹配规则:

location = / { 规则A }

location = /login { 规则B }

location ^~ /static/ { 规则C }

location ~ .(gif|jpg|png|js|css)$ { 规则D }

location ~* .png$ { 规则E }

location / { 规则F }

那么产生的效果如下:

访问根目录/, 比如http://localhost/ 将匹配规则A

访问 http://localhost/login 将匹配规则B,http://localhost/register 则匹配规则F

访问 http://localhost/static/a.html 将匹配规则C

访问 http://localhost/a.gif, http://localhost/b.jpg 将匹配规则D和规则E,但是规则D顺序优先,规则E不起作用,而 http://localhost/static/c.png 则优先匹配到规则C

访问 http://localhost/a.PNG 则匹配规则E,而不会匹配规则D,因为规则E不区分大小写。

访问 http://localhost/category/id/1111则最终匹配到规则F,因为以上规则都不匹配,这个时候应该是nginx转发请求给后端应用服务器,比如FastCGI(php),tomcat(jsp),nginx作为方向代理服务器存在。

- 阅读剩余部分 -

403 forbidden除了权限和索引外的问题

弄了一天的Linux双膝已跪,配置好一切发现网站403

把权限所有都设置了还是这样,顺便一提权限的设置为

chmod -R 777 /home

-R代表着遍历子文件夹 , 777 代表所有权限, 后面是路径

看了一下nginx下的配置文件发现index文件都配好了, 那么问题在哪?

- 阅读剩余部分 -