配置文件

默认路径:/etc/nginx/nginx.conf

  1. user nobody;
  2. worker_processes 1;
  3. error_log logs/error.log;
  4. error_log logs/error.log notice;
  5. error_log logs/error.log info;
  6. pid logs/nginx.pid;
  7. events {
  8. worker_connections 1024;
  9. }
  10. http {
  11. include mime.types;
  12. default_type application/octet-stream;
  13. #log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  14. # '$status $body_bytes_sent "$http_referer" '
  15. # '"$http_user_agent" "$http_x_forwarded_for"';
  16. #access_log logs/access.log main;
  17. sendfile on;
  18. #tcp_nopush on;
  19. #keepalive_timeout 0;
  20. keepalive_timeout 65;
  21. #gzip on;
  22. server {
  23. listen 80;
  24. server_name localhost;
  25. #charset koi8-r;
  26. #access_log logs/host.access.log main;
  27. location / {
  28. root html;
  29. index index.html index.htm;
  30. }
  31. #error_page 404 /404.html;
  32. # redirect server error pages to the static page /50x.html
  33. #
  34. error_page 500 502 503 504 /50x.html;
  35. location = /50x.html {
  36. root html;
  37. }
  38. # proxy the PHP scripts to Apache listening on 127.0.0.1:80
  39. #
  40. #location ~ \.php$ {
  41. # proxy_pass http://127.0.0.1;
  42. #}
  43. # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
  44. #
  45. #location ~ \.php$ {
  46. # root html;
  47. # fastcgi_pass 127.0.0.1:9000;
  48. # fastcgi_index index.php;
  49. # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
  50. # include fastcgi_params;
  51. #}
  52. # deny access to .htaccess files, if Apache's document root
  53. # concurs with nginx's one
  54. #
  55. #location ~ /\.ht {
  56. # deny all;
  57. #}
  58. }
  59. # another virtual host using mix of IP-, name-, and port-based configuration
  60. #
  61. #server {
  62. # listen 8000;
  63. # listen somename:8080;
  64. # server_name somename alias another.alias;
  65. # location / {
  66. # root html;
  67. # index index.html index.htm;
  68. # }
  69. #}
  70. # HTTPS server
  71. #
  72. #server {
  73. # listen 443 ssl;
  74. # server_name localhost;
  75. # ssl_certificate cert.pem;
  76. # ssl_certificate_key cert.key;
  77. # ssl_session_cache shared:SSL:1m;
  78. # ssl_session_timeout 5m;
  79. # ssl_ciphers HIGH:!aNULL:!MD5;
  80. # ssl_prefer_server_ciphers on;
  81. # location / {
  82. # root html;
  83. # index index.html index.htm;
  84. # }
  85. #}
  86. }

常用技巧

上传大文件

nginx

  1. http {
  2. proxy_connect_timeout 300s;
  3. proxy_send_timeout 300s;
  4. proxy_read_timeout 300s;
  5. client_max_body_size 100m;
  6. client_header_timeout 300s;
  7. client_body_timeout 300s;
  8. }
  1. location ~ \.php(.*)$ {
  2. fastcgi_read_timeout 300s;
  3. fastcgi_pass 127.0.0.1:9000;
  4. fastcgi_index index.php;
  5. include fastcgi.conf;
  6. }

php

  1. post_max_size = 100M
  2. upload_max_filesize = 100M
  3. memory_limit = 1024M
  4. max_execution_time = 300
  5. max_input_time = 300

PHP 线程监控

查看 php-fpm 启动时间(可以得出执行了多长时间)

nginx

在相应的虚拟主机配置中添加以下内容

  1. location ~ ^/status$ {
  2. include fastcgi_params;
  3. fastcgi_pass 127.0.0.1:9000;
  4. fastcgi_param SCRIPT_FILENAME $fastcgi_script_name;
  5. }

php-fpm

  1. pm.status_path = /status

配置多个域名

  1. server {
  2. listen 80;
  3. server_name yuwei.cc www.yuwei.cc;#server_name 后跟多个域名用空格隔开即可
  4. }

配置多个站点

借用 Nginx 虚拟主机配置来实现,Nginx 有三种类型的虚拟主机:
基于 IP:需要服务器上有多个地址,每个站点对应不同的地址
基于端口:每个站点对应不同的端口,访问的时候需要后接:port,可以修改 listen 忽略
基于域名:server_name 填写不同的域名即可

  1. server {
  2. listen 80;
  3. server_name w.yuwei.cc;
  4. location / {
  5. root /opt/w;
  6. index index.html;
  7. }
  8. }
  9. server {
  10. listen 80;
  11. server_name ww.yuwei.cc;
  12. location / {
  13. root /opt/ww;
  14. index index.html;
  15. }
  16. }
  17. server {
  18. listen 80;
  19. server_name www.yuwei.cc;
  20. location / {
  21. root /opt/www;
  22. index index.html;
  23. }
  24. }

开启列目录

Nginx 作为文件下载服务器存在时,需要开启 nginx 列目录

  1. server {
  2. location download {
  3. autoindex on;
  4. autoindex_exact_size off;#默认为 on 显示文件的确切大小,单位是 byte,改为 off 显示文件大概大小,单位 KB MB GB
  5. autoindex_localtime on;#默认为 off 显示的文件时间为 GMT 时间,改为 on 后显示文件服务器时间
  6. }
  7. if ($request_filename ~* ^.*?\.(txt|pdf|jpg|png)$) {#列出的指定格式文件需要先下载
  8. add_header Content-Disposition 'attachment';
  9. }
  10. }

禁止 IP 访问

禁止 IP 或未配置的域名访问,可以利用上边所说的default规则,将它们都转到 404 页面去

  1. server {
  2. listen 80 default;
  3. server_name _;
  4. return 404;
  5. }

上述方法较粗暴,可以将它们直接301重定向到指定的域名,相当于给站点增加了外链

  1. server {
  2. rewrite ^/(.*)$ https://yuwei.cc/$1 permanent;
  3. }

配置默认站点

如果 Nginx 服务器上创建了多个虚拟主机,默认会从上到下查找,匹配不到虚拟主机则会返回第一个虚拟主机;如果想指定一个默认站点时,可以将这个站点的虚拟主机放在配置文件中第一个虚拟主机的位置,或者在这个站点的虚拟主机上配置listen port default

  1. server {
  2. listen 80 default;
  3. }

404 跳转到首页

网站出现 404页面不是特别友好,我们可以通过上边的配置在出现404之后给自动跳转到首页去

返回验证文件

微信支付等服务需要放一个txt 文件到服务器上进行验证,可以直接修改配置而无需把文件传到服务器指定位置

  1. location = /XGFyic8tZB.txt {
  2. default_type text/plain;
  3. return 200 'd6399a83657vb225615c31c10684e6ac';
  4. }