nginx.conf 设置

🌙
手机阅读
本文目录结构

刚配置 gzip 压缩的

路径:/etc/nginx/nginx.conf 内容如下:

  1. user nginx;
  2. worker_processes 1;
  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. log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  12. '$status $body_bytes_sent "$http_referer" '
  13. '"$http_user_agent" "$http_x_forwarded_for"';
  14. access_log /var/log/nginx/access.log main;
  15. sendfile on;
  16. #tcp_nopush on;
  17. keepalive_timeout 65;
  18. #gzip on;
  19. gzip on; #开启gzip
  20. gzip_min_length 1k; #低于1kb的资源不压缩
  21. gzip_comp_level 8; #压缩级别【1-9】,越大压缩率越高,同时消耗cpu资源也越多,建议设置在4左右。
  22. gzip_types text/plain application/javascript application/x-javascript text/javascript text/xml text/css; #需要压缩哪些响应类型的资源,多个空格隔开。不建议压缩图片,下面会讲为什么。
  23. gzip_disable "MSIE [1-6]\."; #配置禁用gzip条件,支持正则。此处表示ie6及以下不启用gzip(因为ie低版本不支持)
  24. gzip_vary on; #是否添加“Vary: Accept-Encoding”响应头
  25. include /etc/nginx/conf.d/*.conf;
  26. }

文件 /etc/nginx/conf.d/default.conf 如下

  1. server {
  2. listen 80;
  3. server_name localhost;
  4. #charset koi8-r;
  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. }

www 重定向

www 重定向到转向不带 www:

  1. server {
  2. server_name www.axihe.com;
  3. return 301 $scheme://axihe.com$request_uri;
  4. }

不带 www 重定向到带 www:

  1. server {
  2. server_name axihe.com;
  3. return 301 $scheme://www.axihe.com$request_uri;
  4. }

/etc/nginx/conf.d/default.conf文件如下

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

/etc/nginx/conf.d/www-axihe-com文件如下

  1. server {
  2. listen 80;
  3. server_name www.axihe.com;
  4. root /usr/share/nginx/axihe-web/dist;
  5. index index.html;
  6. location ~* ^.+\.(jpg|jpeg|gif|png|ico|css|js|pdf|txt){
  7. root /usr/share/nginx/axihe-web/dist;
  8. }
  9. }

ssl setting

  1. server {
  2. listen 80;
  3. listen 443 ssl; # 对 443 端口进行 SSL 加密
  4. server_name www.axihe.com;
  5. root /usr/share/nginx/axihe-web/dist;
  6. index index.html;
  7. location ~* ^.+\.(jpg|jpeg|gif|png|ico|css|js|pdf|txt){
  8. root /usr/share/nginx/axihe-web/dist;
  9. }
  10. # 沃通生成的 SSL 证书的存放位置
  11. ssl_certificate /etc/nginx/cert/1874900_www.axihe.com.pem;
  12. ssl_certificate_key /etc/nginx/cert/1874900_www.axihe.com.key;
  13. # 其他 SSL 相关设置
  14. ssl_session_timeout 10m;
  15. ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
  16. ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
  17. ssl_prefer_server_ciphers on;
  18. # 主域名和子域名都启用 HSTS,过期时间为两年
  19. add_header Strict-Transport-Security "max-age=63072000; includeSubdomains; preload";
  20. # 所有 HTTP 访问都永久重定向(301)到 HTTPS
  21. if ( $scheme = http ) {
  22. rewrite ^/(.*) https://$server_name/$1 permanent;
  23. }
  24. }

增加谷歌分析

/etc/nginx/conf.d/www-axihe-com文件如下

  1. server {
  2. listen 80;
  3. listen 443 ssl; # 对 443 端口进行 SSL 加密
  4. server_name www.axihe.com;
  5. # root /usr/share/nginx/axihe-web/dist;
  6. # index index.html;
  7. # google any start
  8. userid on;
  9. userid_name cid;
  10. userid_domain axihe.com;
  11. userid_path /;
  12. userid_expires max;
  13. location @ga {
  14. internal;
  15. resolver 8.8.8.8 [2001:4860:4860::8888];
  16. set $lang 'zh-cn';
  17. if ( $http_accept_language ~ en ) {
  18. set $lang 'en-us';
  19. }
  20. proxy_method GET;
  21. # UA-100341141-2
  22. proxy_pass https://ssl.google-analytics.com/collect?v=1&tid=UA-100341141-2&$uid_set$uid_got&t=pageview&dh=$host&dp=$uri&uip=$remote_addr&dr=$http_referer&z=$msec&ul=$lang;
  23. proxy_set_header User-Agent $http_user_agent;
  24. proxy_pass_request_headers off;
  25. proxy_pass_request_body off;
  26. }
  27. # google any end
  28. location / {
  29. root /usr/share/nginx/axihe-web/dist;
  30. index index.html;
  31. # 当匹配到此 location 时,这里会异步的调用 @ga
  32. if ($http_user_agent !~* "qihoobot|Baiduspider|Googlebot|Googlebot-Mobile|Googlebot-Image|Mediapartners-Google|Adsbot-Google|Feedfetcher-Google|Yahoo! Slurp|Yahoo! Slurp China|YoudaoBot|Sosospider|Sogou spider|Sogou web spider|MSNBot|ia_archiver|Tomato Bot|YiSou Spider") {
  33. post_action @ga;
  34. }
  35. # post_action @ga;
  36. }
  37. error_page 404 /404.html;
  38. # 沃通生成的 SSL 证书的存放位置
  39. ssl_certificate /etc/nginx/cert/1874900_www.axihe.com.pem;
  40. ssl_certificate_key /etc/nginx/cert/1874900_www.axihe.com.key;
  41. # 其他 SSL 相关设置
  42. ssl_session_timeout 10m;
  43. ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
  44. ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
  45. ssl_prefer_server_ciphers on;
  46. # 主域名和子域名都启用 HSTS,过期时间为两年
  47. add_header Strict-Transport-Security "max-age=63072000; includeSubdomains; preload";
  48. # 所有 HTTP 访问都永久重定向(301)到 HTTPS
  49. if ( $scheme = http ) {
  50. rewrite ^/(.*) https://$server_name/$1 permanent;
  51. }
  52. }

2020-07

  1. server {
  2. listen 443 ssl; # 对 443 端口进行 SSL 加密
  3. server_name www.axihe.com;
  4. # root /usr/share/nginx/axihe-web/dist;
  5. # index index.html;
  6. location / {
  7. root /usr/share/nginx/axihe-web/dist;
  8. index index.html;
  9. }
  10. error_page 404 /404.html;
  11. # 沃通生成的 SSL 证书的存放位置
  12. ssl_certificate /etc/nginx/cert/3485712_www.axihe.com.pem;
  13. ssl_certificate_key /etc/nginx/cert/3485712_www.axihe.com.key;
  14. # 其他 SSL 相关设置
  15. ssl_session_timeout 10m;
  16. ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
  17. ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
  18. ssl_prefer_server_ciphers on;
  19. # 主域名和子域名都启用
  20. add_header Strict-Transport-Security "max-age=31536000; includeSubdomains; preload";
  21. # 所有 HTTP 访问都永久重定向(301)到 HTTPS
  22. if ( $scheme = http ) {
  23. rewrite ^/(.*) https://$server_name/$1 permanent;
  24. }
  25. }

AXIHE / 精选资源

浏览全部教程

面试题

学习网站

前端培训
自己甄别

前端书籍

关于朱安邦

我叫 朱安邦,阿西河的站长,在杭州。

以前是一名平面设计师,后来开始接接触前端开发,主要研究前端技术中的JS方向。

业余时间我喜欢分享和交流自己的技术,欢迎大家关注我的 Bilibili

关注我: Github / 知乎

于2021年离开前端领域,目前重心放在研究区块链上面了

我叫朱安邦,阿西河的站长

目前在杭州从事区块链周边的开发工作,机械专业,以前从事平面设计工作。

2014年底脱产在老家自学6个月的前端技术,自学期间几乎从未出过家门,最终找到了满意的前端工作。更多>

于2021年离开前端领域,目前从事区块链方面工作了