资源

官方文档
http://nginx.org/en/docs/

基本使用
https://nginx.org/en/docs/beginners_guide.html

一些实栗
http://xstarcd.github.io/wiki/sysadmin/nginx_confs.html


启动/停止/重启等

To start nginx, run the executable file.
Once nginx is started, it can be controlled by invoking the executable with the -s parameter. Use the following syntax:

nginx -s signal
Where signal may be one of the following:
·
stop — fast shutdown
·
quit — graceful shutdown
·
reload — reloading the configuration file
·
reopen — reopening the log files

配置

configure file location: /etc/nginx/nginx.conf

可能的默认配置:
server root : /usr/share/nginx/html
error_log: /var/log/nginx/error.log
access_log: /var/log/nginx/access.log

location配置

  1. server {
  2. location / {
  3. proxy_pass http://localhost:8080;
  4. }
  5. location /images/ {
  6. root /data;
  7. }
  8. }

关于URL尾部的/有三点也需要说明一下。第一点与location配置有关,其他两点无关。
location中的字符有没有/都没有影响。也就是说/user/和/user是一样的。
如果URL结构是https://domain.com/的形式,尾部有没有/都不会造成重定向。因为浏览器在发起请求的时候,默认加上了/。虽然很多浏览器在地址栏里也不会显示/。这一点,可以访问baidu验证一下。
如果URL的结构是https://domain.com/some-dir/。尾部如果缺少/将导致重定向。因为根据约定,URL尾部的/表示目录,没有/表示文件。所以访问/some-dir/时,服务器会自动去该目录下找对应的默认文件。如果访问/some-dir的话,服务器会先去找some-dir文件,找不到的话会将some-dir当成目录,重定向到/some-dir/,去该目录下找默认文件。可以去测试一下你的网站是不是这样的。
https://segmentfault.com/a/1190000013267839

location详细规则: http://nginx.org/en/docs/http/ngx_http_core_module.html#location
rewrite规则: http://nginx.org/en/docs/http/ngx_http_rewrite_module.html#rewrite

Setting the root directive within a location block is bad practice
it should be well known by now that if is evil and often produces problems

A location can either be defined by a prefix string, or by a regular expression.
Regular expressions are specified with the preceding “~*” modifier (for case-insensitive matching),
or the “~” modifier (for case-sensitive matching).

PHP FastCGI

https://www.nginx.com/resources/wiki/start/topics/examples/phpfcgi/

  1. location / {
  2. index index.php index.html index.htm;
  3. try_files $uri $uri/ /index.php index.php;
  4. }
  5. location ~* \.php$ {
  6. fastcgi_pass 127.0.0.1:9000;
  7. fastcgi_index index.php;
  8. fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  9. include fastcgi_params;
  10. }


$document_root
root or alias directive’s value for the current request
$request_filename
file path for the current request, based on the root or alias directives, and the request URI
http://nginx.org/en/docs/http/ngx_http_core_module.html