使用nginx时常见的错误以及解决办法

来源: 海外服务器
导读 在使用Nginx的过程中,无论是新手还是经验丰富的管理员,都可能遇到一些典型的配置或运行错误,导致网站无法正常访问或性能下降。常见的错误包括因语法错误或符号遗漏导致的配置失效、权限不足造成的403禁止访问、端口被占用引发的启动失败,以及上游服务无法连接等问题。这些状况往往令人困扰,但大多可以通过系统性的排查快速解决。

许多人都可以遇到一个陷阱。下面我们列出,我们常常看到的问题,以及表明如何办理这些问题。在Freenode上的#nginx IRC频道这些接头很频繁。

1、权限

从来不要利用777权限,查察目次的权限

2、root配置

BAD:

server{server_namewww.example.com;location/{root/var/www/nginx-default/;#[...]}location/foo{root/var/www/nginx-default/;#[...]}location/bar{root/var/www/nginx-default/;#[...]}}

GOOD:

server{server_namewww.example.com;root/var/www/nginx-default/;location/{#[...]}location/foo{#[...]}location/bar{#[...]}}3、索引配置

BAD:

http{indexindex.phpindex.htmindex.html;server{server_namewww.example.com;location/{indexindex.phpindex.htmindex.html;#[...]}}server{server_nameexample.com;location/{indexindex.phpindex.htmindex.html;#[...]}location/foo{indexindex.php;#[...]}}}

GOOD:

http{indexindex.phpindex.htmindex.html;server{server_namewww.example.com;location/{#[...]}}server{server_nameexample.com;location/{#[...]}location/foo{#[...]}}}4、Using If

if 是邪恶的 拜见If Is Evil

5、Server Name (If)

BAD:

server{server_nameexample.com*.example.com;if($host~*^www.(.+)){set$raw_domain$1;rewrite^/(.*)$$raw_domain/$1permanent;}#[...]}}

每次都要检测主机头,这是低效的,英国服务器俄罗斯主机,你应该制止,推荐利用下面的

GOOD: