nginx静态资源配置

Posted 源记杂事屋

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了nginx静态资源配置相关的知识,希望对你有一定的参考价值。

前言

之前网站在建站的时候,本来是想通过spring boot代码上传图片,后面由于maven打包不方便

于是采用nginx静态资源转发的办法,使用CDN作为网站图片连接


配置


  • 安装


yum install -y nginx


  • 启动nginx服务

systemctl start nginx.service
  • 查看nginx配置目录


ps -ef | grep nginx


master process 后面的目录就是nginx的目录


  • 重启nginx命令


nginx -s reload
  • 以下是个人nginx配置


# For more information on configuration, see:# * Official English Documentation: http://nginx.org/en/docs/# * Official Russian Documentation: http://nginx.org/ru/docs/
user nginx;worker_processes auto;error_log /var/log/nginx/error.log;#pid /run/nginx.pid;
# Load dynamic modules. See /usr/share/nginx/README.dynamic.include /usr/share/nginx/modules/*.conf;
events { worker_connections 1024;}
http { log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout 65; types_hash_max_size 2048;
include /etc/nginx/mime.types; default_type application/octet-stream;
# Load modular configuration files from the /etc/nginx/conf.d directory. # See http://nginx.org/en/docs/ngx_core_module.html#include # for more information. include /etc/nginx/conf.d/*.conf;
server { listen 8090 default_server; listen [::]:8090 default_server; server_name _; root /usr/share/nginx/html;
# Load configuration files for the default server block. include /etc/nginx/default.d/*.conf; location ~ .*\.(gif|jpg|jpeg|png)$ { expires 24h; root /home/images/;#指定图片存放路径 access_log /etc/nginx/log/access_log.log;#日志存放路径 proxy_store on; proxy_store_access user:rw group:rw all:rw; proxy_temp_path /home/images/;#图片访问路径 proxy_redirect off; proxy_set_header Host 127.0.0.1; client_max_body_size 10m; client_body_buffer_size 1280k; proxy_connect_timeout 900; proxy_send_timeout 900; proxy_read_timeout 900; proxy_buffer_size 40k; proxy_buffers 40 320k; proxy_busy_buffers_size 640k; proxy_temp_file_write_size 640k; if ( !-e $request_filename) { proxy_pass http://127.0.0.1;#默认80端口 } } location / { }
error_page 404 /404.html; location = /40x.html { }
error_page 500 502 503 504 /50x.html; location = /50x.html { } }
# Settings for a TLS enabled server.## server {# listen 443 ssl http2 default_server;# listen [::]:443 ssl http2 default_server;# server_name _;# root /usr/share/nginx/html;## ssl_certificate "/etc/pki/nginx/server.crt";# ssl_certificate_key "/etc/pki/nginx/private/server.key";# ssl_session_cache shared:SSL:1m;# ssl_session_timeout 10m;# ssl_ciphers HIGH:!aNULL:!MD5;# ssl_prefer_server_ciphers on;## # Load configuration files for the default server block.# include /etc/nginx/default.d/*.conf;## location / {# }## error_page 404 /404.html;# location = /40x.html {# }## error_page 500 502 503 504 /50x.html;# location = /50x.html {# }# }
}


  • nginx监听8090端口

listen 8001 default_server;



  • nginx安装路径

/etc/nginx/


  • 静态资源路径

/usr/share/nginx/html;



以上是本站在建站过程中配置nginx的基本过程,后续继续完善以及展示一些遇到的问题以及解决方案。


注:以上操作环境为:阿里云linux服务器,版本:centos7

以上是关于nginx静态资源配置的主要内容,如果未能解决你的问题,请参考以下文章

Nginx伪静态配置和常用Rewrite伪静态规则

Nginx修改静态文件访问路径

Yii2 配置 Nginx 伪静态

nginx + tomcat 配置,静态资源直接使用nginx

Nginx静态资源服务器搭建

Nginx静态资源服务器搭建