参考

基本命令

  • 安装
  1. yum install nginx
  • 查看安装目录

    1. whereis nginx
  • 查看版本号

  1. nginx -v
  2. nginx -V ## 查询版本及配置
  • 重启nginx
  1. nginx -s reload
  • 验证配置是否正确
  1. ./nginx -t
  • Nginx正常启动:
  1. nginx
  • 快速停止或关闭Nginx
  1. ./nginx -s stop
  • 正常停止或关闭Nginx
  1. ./nginx -s quit
  • 配置文件修改重装载命令
  1. ./nginx -s reload

基本配置

完整的配置说明

image.png

目录结构

  1. |—— nginx.conf # 配置入口文件
  2. ├── conf.d # 放置所有的配置
  3. ├── default.conf # http配置,通过307定向到https
  4. ├── default_ssl.conf # https配置
  5. ├── ... # 更多的配置...

nginx.conf

  1. user root;
  2. # 设置工作进程的数量
  3. worker_processes 1;
  4. error_log /var/log/nginx/error.log;
  5. pid /run/nginx.pid;
  6. include /usr/share/nginx/modules/*.conf;
  7. events {
  8. #==最大连接数,一般设置为cpu*2048
  9. worker_connections 1024;
  10. }
  11. http {
  12. # 文件拓展名查找集合
  13. include /etc/nginx/mime.types;
  14. # 当查找不到对应类型的时候默认值
  15. default_type application/octet-stream;
  16. log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  17. '$status $body_bytes_sent "$http_referer" '
  18. '"$http_user_agent" "$http_x_forwarded_for"';
  19. access_log /var/log/nginx/access.log main;
  20. # 调用 sendfile 系统传输文件
  21. sendfile on;
  22. #tcp_nopush on;
  23. # ==客户端链接超时时间
  24. # keepalive_timeout 0;
  25. keepalive_timeout 65;
  26. # 开启gzip 压缩
  27. gzip on;
  28. # 设置gzip所需的http协议最低版本 (HTTP/1.1, HTTP/1.0)
  29. gzip_http_version 1.1;
  30. # 设置压缩级别,压缩级别越高压缩时间越长 (1-9)
  31. gzip_comp_level 4;
  32. # 设置压缩的最小字节数, 页面Content-Length获取
  33. gzip_min_length 1000;
  34. # 设置压缩文件的类型 (text/html)
  35. gzip_types text/plain application/javascript text/css;
  36. # 加载/etc/nginx/conf.d/下的所有配置
  37. include /etc/nginx/conf.d/*.conf;
  38. }

default.conf

/etc/nginx/conf.d/default.conf;

  1. server {
  2. #站点监听端口
  3. listen 80 default_server;
  4. listen [::]:80 default_server; #站点访问域名
  5. server_name forguo.cn www.forguo.cn;
  6. #编码格式,避免url参数乱码
  7. charset utf-8;
  8. # 重定向到https
  9. location / {
  10. #location用来匹配同一域名下多个URI的访问规则
  11. #比如动态资源如何跳转,静态资源如何跳转等
  12. #location后面跟着的/代表匹配规则
  13. return 307 https://forguo.cn$request_uri;
  14. }
  15. }

default_ssl.conf

/etc/nginx/conf.d/default_ssl.conf;

  1. server {
  2. listen 443 ssl http2; # 加一句 http2.
  3. server_name forguo.cn www.forguo.cn;
  4. ssl_certificate forguo.cn.crt;
  5. ssl_certificate_key forguo.cn.key;
  6. ssl_session_cache shared:SSL:10m;
  7. ssl_session_timeout 10m;
  8. ssl_ciphers EECDH+CHACHA20:EECDH+AES128:RSA+AES128:EECDH+AES256:RSA+AES256:EECDH+3DES:RSA+3DES:!MD5;
  9. ssl_prefer_server_ciphers on;
  10. location / {
  11. #站点根目录,可以是相对路径,也可以使绝对路径
  12. root /root/www/;
  13. #默认首页文件
  14. index index.html index.htm;
  15. # 如果 URL 匹配不到任何静态资源,则应该返回同一个 index.html 页面
  16. try_files $uri $uri/ /index.html;
  17. #拒绝请求,返回403,一般用于某些目录禁止访问
  18. #deny all;
  19. #允许请求
  20. #allow all;
  21. # CORS
  22. add_header 'Access-Control-Allow-Origin' '*';
  23. add_header 'Access-Control-Allow-Credentials' 'true';
  24. add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
  25. add_header 'Access-Control-Allow-Headers' 'DNT, X-CustomHeader, Keep-Alive, User-Agent, X-Requested-With, If-Modified-Since, Cache-Control, Content-Type';
  26. }
  27. #error_page 404 /404.html;
  28. # redirect server error pages to the static page /50x.html
  29. #
  30. error_page 500 502 503 504 /50x.html;
  31. location = /50x.html {
  32. root html;
  33. }
  34. # 转发api到node服务
  35. location /api/ {
  36. proxy_pass http://127.0.0.1:3333/api/;
  37. }
  38. # 开启目录浏览功能
  39. location /brower/ {
  40. charset utf-8;
  41. # 浏览/root/www/brower/ 目录,只需要配置上一级即可
  42. root /root/www/;
  43. # alias /root/www/;
  44. autoindex on;
  45. # 开启目录浏览功能;
  46. autoindex_exact_size off;
  47. # 关闭详细文件大小统计,让文件大小显示MB,GB单位,默认为b;
  48. autoindex_localtime on;
  49. # 开启以服务器本地时区显示文件修改日期!
  50. }
  51. }

Docker 容器中的Nginx配置Demo

nginx.conf

  1. user nginx;
  2. worker_processes auto;
  3. error_log /var/log/nginx/error.log warn;
  4. pid /var/run/nginx.pid;
  5. events {
  6. worker_connections 1024;
  7. }
  8. http {
  9. include /etc/nginx/mime.types;
  10. default_type application/octet-stream;
  11. access_log /var/log/nginx/access.log;
  12. #配置了多个server的时候,一定要设置该参数
  13. server_names_hash_bucket_size 64;
  14. #处理请求request_line/request_header缓存大小,首先根据client_header_buffer_size分配大小,大小超出后会根据large_client_header_buffers分配大小
  15. #所以基本client_header_buffer_size用来应对小的request_line/request_header,large_client_header_buffers应对大的请求
  16. #如果http主配置没有设置,在server中配置的话,需要设置listen 80 default_server;默认服务,因为在处理请求头信息的时候,还没解析到server去,直接从默认服务取值
  17. client_header_buffer_size 32k;
  18. large_client_header_buffers 4 32k;
  19. #设置请求主体内存大小,文件上传很容易受影响
  20. client_max_body_size 50m;
  21. #开启高效传输文件模式
  22. sendfile on;
  23. #优化带宽利用率
  24. tcp_nopush on;
  25. tcp_nodelay on;
  26. #大文件传输的时候,需要兼顾,太大的话,普通请求完成了又不释放http连接,占用worker_connections,worker_connections达到上限后会宕机
  27. keepalive_timeout 60;
  28. #优化fastcgi
  29. fastcgi_connect_timeout 60;
  30. fastcgi_send_timeout 60;
  31. fastcgi_read_timeout 60;
  32. fastcgi_buffer_size 4k;
  33. fastcgi_buffers 8 4k;
  34. fastcgi_busy_buffers_size 8k;
  35. fastcgi_temp_file_write_size 256k;
  36. #server
  37. #{
  38. # listen 80 default;
  39. # return 403;
  40. #}
  41. # 加载/etc/nginx/conf.d/下的所有配置
  42. include /etc/nginx/conf.d/*.conf;
  43. }

default.conf

/etc/nginx/conf.d/default.conf内容

  1. server {
  2. listen 80;
  3. listen [::]:80;
  4. server_name localhost;
  5. #access_log /var/log/nginx/host.access.log main;
  6. location / {
  7. root /usr/share/nginx/html;
  8. index index.html index.htm;
  9. }
  10. #error_page 404 /404.html;
  11. # redirect server error pages to the static page /50x.html
  12. #
  13. error_page 500 502 503 504 /50x.html;
  14. location = /50x.html {
  15. root /usr/share/nginx/html;
  16. }
  17. # proxy the PHP scripts to Apache listening on 127.0.0.1:80
  18. #
  19. #location ~ \.php$ {
  20. # proxy_pass http://127.0.0.1;
  21. #}
  22. # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
  23. #
  24. #location ~ \.php$ {
  25. # root html;
  26. # fastcgi_pass 127.0.0.1:9000;
  27. # fastcgi_index index.php;
  28. # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
  29. # include fastcgi_params;
  30. #}
  31. # deny access to .htaccess files, if Apache's document root
  32. # concurs with nginx's one
  33. #
  34. #location ~ /\.ht {
  35. # deny all;
  36. #}
  37. }