一些nginx的配置怎么样的才算好呢
权限777 还是很不安全的 如果能不用 777 最好还是不用吧
下面看这两段代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
server { server_name www.example.com; location / { root /var/www/nginx-default/; # [...] } location /foo { root /var/www/nginx-default/; # [...] } location /bar { root /var/www/nginx-default/; # [...] } } |
如果是多目录最好把root写在一条配置上
1 2 3 4 5 6 7 8 9 10 11 12 13 |
server { server_name www.example.com; root /var/www/nginx-default/; location / { # [...] } location /foo { # [...] } location /bar { # [...] } } |
下面也是
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
http { index index.php index.htm index.html; server { server_name www.example.com; location / { index index.php index.htm index.html; # [...] } } server { server_name example.com; location / { index index.php index.htm index.html; # [...] } } } |
还是把重复的index提出来吧
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
http { index index.php index.htm index.html; server { server_name www.example.com; location / { # [...] } } server { server_name example.com; location / { # [...] } } } |
………….
下面来看下if 。本人以前用if做判断域名 结果发现效率蛮低的。如果可以的话在配置中 不要用if
见网上好多教程 –都在域名 那里做判断 结果呢代码是少写了很多行效率真不敢恭维
1 2 3 4 5 6 7 8 9 |
server { server_name example.com *.example.com; if ($host ~* ^www\.(.+)) { set $raw_domain $1; rewrite ^/(.*)$ $raw_domain/$1 permanent; } # [...] } } |
可以换成以下的
1 2 3 4 5 6 7 8 |
server { server_name www.example.com; return 301 $scheme://example.com$request_uri; } server { server_name example.com; # [...] } |
…
禁访问一些请求request也可以换成try_files
1 2 3 4 5 6 7 8 |
server { root /var/www/example.com; location / { if (!-f $request_filename) { break; } } } |
GOOD:
1 2 3 4 5 6 |
server { root /var/www/example.com; location / { try_files $uri $uri/ /index.html; } } |
————————
这里的fastcgi_param 最好用$document_root 不要直接用路径
1 |
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; |
nginx如果网站存在默认的root根目录 升级的话会root会被 清空的哦
rewrite的时候 写上http://
如果可以不用正则 就不要用在nginx.conf中
BAD:
1 |
rewrite ^/(.*)$ http://example.com/$1 permanent; |
GOOD:
1 |
rewrite ^ http://example.com$request_uri? permanent; |
BETTER:
1 |
return 301 http://example.com$request_uri; |
反向代理
Proxy一般我们都是这样配置的
1 2 3 4 5 6 7 8 9 |
server { server_name _; root /var/www/site; location / { include fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_pass unix:/tmp/phpcgi.socket; } } |
下面两个是官方给推荐的
1 2 3 4 5 6 7 8 9 10 11 12 |
server { server_name _; root /var/www/site; location / { try_files $uri $uri/ @proxy; } location @proxy { include fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_pass unix:/tmp/phpcgi.socket; } } |
1 2 3 4 5 6 7 8 9 10 11 12 |
server { server_name _; root /var/www/site; location / { try_files $uri $uri/ /index.php; } location ~ \.php$ { include fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_pass unix:/tmp/phpcgi.socket; } } |
php中ini找到以下参数改为 cgi.fix_pathinfo=0