nginx代理静态资源
项目需求:网页响应过慢,需要nginx代理静态资源
安装nginx
参考了这篇博客进行安装:ubuntu上nginx详细安装部署教程 - 敲代码的椰子 - 博客园
但是他使用的安装包有些已经旧了,可以在官网上复制最新安装包的链接 来替换命令中的。
1、cd /usr/local/src
2、下载相关组件
wget http://nginx.org/download/nginx-1.16.0.tar.gz wget https://www.openssl.org/source/openssl-fips-2.0.16.tar.gz wget http://zlib.net/zlib-1.2.11.tar.gz wget https://netix.dl.sourceforge.net/project/pcre/pcre/8.40/pcre-8.40.tar.gz
3、安装nginx相关组件
安装openssl
tar zxvf openssl-fips-2.0.16.tar.gz cd openssl-fips-2.0.16 ./config && make && make install
安装pcre
tar zxvf pcre-8.40.tar.gz cd pcre-8.40 ./configure && make && make install
安装zlib
tar zxvf zlib-1.2.11.tar.gz cd zlib-1.2.11 ./configure && make && make install
4、安装nginx
tar zxvf nginx-1.16.0.tar.gz cd nginx-1.16.0 ./configure && make && make install
三、启动nginx
1、启动nginx
方式1: /usr/local/nginx/sbin/nginx
方式2 cp /usr/local/nginx/sbin/nginx /usr/local/bin/
nginx
2、查看nginx是否启动成功
方式1:netstat -lnp
方式2:lsof -i:80
3、基本操作
/usr/local/nginx/sbin/nginx#启动 /usr/local/nginx/sbin/nginx -s stop(quit、reload)#停止/重启 /usr/local/nginx/sbin/nginx -h#命令帮助 vi /usr/local/nginx/conf/nginx.conf#配置文件
4、nginx相关的目录
日志 /usr/local/nginx/logs/
配置 /usr/local/nginx/conf/nginx.conf
静态资源目录 /usr/local/nginx/html/
nginx代理静态资源的配置
修改/usr/local/nginx/conf/nginx.conf,全部放在http模块中
upstream backend{ 为后端服务起一个名字
server 127.0.0.1:4430; 后端服务的ip:端口
}
server {
listen 80; nginx监听80端口
access_log /usr/local/nginx/logs/proxy.log main; 指定日志文件
server_name 这里要配置域名;
location ~* \.(svg|png|css|js)$ { 后缀匹配
root /usr/local/nginx/html/; 将路由进行转发到这个目录下
}
root的处理结果是:root路径+location路径
alias的处理结果是:使用alias路径替换location路径
alias是一个目录别名的定义,root则是最上层目录的定义。
还有一个重要的区别是alias后面必须要用“/”结束,否则会找不到文件的
location / {
proxy_ssl_session_reuse off;
proxy_ssl_name $host;
proxy_ssl_server_name on;
proxy_set_header Host $host;
proxy_ssl_verify off;
proxy_pass https://backend;
}
}
server {
listen 443 ssl;
access_log on;
ssl_certificate /root/my.crt; 证书存放位置
ssl_certificate_key /root/my.key; 私钥存放位置
access_log /usr/local/nginx/logs/proxy.log main;
server_name 这里要配置域名;
location ~* \.(css|js|png|svg)$ {
root /usr/local/nginx/html/
}
location / {
proxy_ssl_session_reuse off;
proxy_ssl_name $host;
proxy_ssl_server_name on;
proxy_set_header Host $host;
proxy_ssl_verify off;
proxy_pass https://backend;
}
}
假设我的域名是guolicheng.cn
实际访问一个静态文件: http://cat.guolicheng.cn/static/media/meng.png
nginx 中 匹配到后缀,并截取location为/static/media/meng.png
转发到 /usr/local/nginx/html/static/media/meng.png,并返回这个文件
遇到的问题
我在配置过程中遇到的问题,由于项目原因 nginx和原server必须部署在同一台机器上
1 原server监听80和443端口,跟nginx也需要在80端口上运行,导致冲突,解决方法:
使用docker运行原server,并进行docker到宿主机的端口映设,例如
docker run -it -p 8000:80 -p 4430:4433 serverimage:release
代表将docker中的80端口映设到宿主机的8000端口,之后在nginx配置中将业务转发到8000和4430端口即可
2 在nginx监听443端口遇到问题unknown directive "ssl" ,前提是已经有证书和私钥
解决方法:
- 下载openssl库:
- wget https://www.openssl.org/source/openssl-1.1.1k.tar.gz
- wget https://www.openssl.org/source/openssl-fips-2.0.16.tar.gz
- tar zxvf openssl-fips-2.0.16.tar.gz
- cd openssl-fips-2.0.16
- ./configure
- 在/usr/loacl/src/的nginx解压目录下,执行./configure --with-http_ssl_module
- 执行make命令,但是不要执行make install,因为make是用来编译的,而make install是安装,不然你整个nginx会重新覆盖的。
- 在我们执行完做命令后,我们可以查看到在nginx解压目录下,objs文件夹中多了一个nginx的文件,这个就是新版本的程序了。首先我们把之前的nginx先备份一下,然后把新的程序复制过去覆盖之前的即可。
cp /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.bak
cp objs/nginx /usr/local/nginx/sbin/nginx
参考:https://blog.csdn.net/weixin_38111957/article/details/81283121
配置代码:
#user nobody;
worker_processes 1;#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;#pid logs/nginx.pid;events {worker_connections 1024;
}http {include mime.types;default_type application/octet-stream;log_format main '$remote_addr - $remote_user [$time_local] "$request" ''$status $body_bytes_sent "$http_referer" ''"$http_user_agent" "$http_x_forwarded_for"''$upstream_addr $upstream_status $request_uri';#access_log logs/access.log main;sendfile on;#tcp_nopush on;#keepalive_timeout 0;keepalive_timeout 65;#gzip on;server {listen 80;server_name localhost;#charset koi8-r;#access_log logs/host.access.log main;location / {root html;index index.html index.htm;}#error_page 404 /404.html;# redirect server error pages to the static page /50x.html#error_page 500 502 503 504 /50x.html;location = /50x.html {root html;}# proxy the PHP scripts to Apache listening on 127.0.0.1:80##location ~ \.php$ {# proxy_pass http://127.0.0.1;#}# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000##location ~ \.php$ {# root html;# fastcgi_pass 127.0.0.1:9000;# fastcgi_index index.php;# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;# include fastcgi_params;#}# deny access to .htaccess files, if Apache's document root# concurs with nginx's one##location ~ /\.ht {# deny all;#}}upstream backend{server 127.0.0.1:4430;}server {listen 80;access_log /usr/local/nginx/logs/proxy.log main;server_name 这里要配置域名;location ~* \.(svg|png|css|js)$ {root /usr/local/nginx/html/;}location / {proxy_ssl_session_reuse off;proxy_ssl_name $host;proxy_ssl_server_name on;proxy_set_header Host $host;proxy_ssl_verify off;proxy_pass https://backend;}}server {listen 443 ssl;access_log on;ssl_certificate /root/my.crt;ssl_certificate_key /root/my.key;access_log /usr/local/nginx/logs/proxy.log main;server_name 这里要配置域名;location ~* \.(css|js|png|svg)$ {root /usr/local/nginx/html/}location / {proxy_ssl_session_reuse off;proxy_ssl_name $host;proxy_ssl_server_name on;proxy_set_header Host $host;proxy_ssl_verify off;proxy_pass https://backend;}}# another virtual host using mix of IP-, name-, and port-based configuration##server {# listen 8000;# listen somename:8080;# server_name somename alias another.alias;# location / {# root html;# index index.html index.htm;# }#}# HTTPS server##server {# listen 443 ssl;# server_name localhost;# ssl_certificate cert.pem;# ssl_certificate_key cert.key;# ssl_session_cache shared:SSL:1m;# ssl_session_timeout 5m;# ssl_ciphers HIGH:!aNULL:!MD5;# ssl_prefer_server_ciphers on;# location / {# root html;# index index.html index.htm;# }#}}
nginx代理静态资源相关推荐
- nginx 代理静态资源 R_CONTENT_LENGTH_MISMATCH 200
nginx 代理静态资源突然出现一些静态资源文件加载不到(浏览器F12 控制台显示R_CONTENT_LENGTH_MISMATCH 200),在浏览器地址栏直接访问静态资源文件又能加载到: 查看了n ...
- nginx 代理静态资源报 403
用tomcat跑了一个上传服务,文件上传到指定nginx的html目录,用nginx来代理静态资源,结果上传能够成功,访问却报403. 解决办法,将html的拥有者改成nobody: chown -R ...
- 为什么用Nginx处理静态资源
从饭店说为什么用Nginx处理静态资源 什么是静态资源 什么是动态资源 开饭店的几个阶段 小饭店,单打独斗 客人变多,招服务员 一个大厨忙不过来,招大厨(乱入超纲) Nginx对静态资源的处理 大半夜 ...
- 入门Nginx之-静态资源服务器及跨域配置
简介 这里静态资源就以之前的一个项目文章地址为例,源码 Github,项目本身很简单,只是分别对第三方的服务端.自己的服务端发起请求. 不论是调用第三方服务端接口,还是自己的后端服务,如果跨域未在服务 ...
- linux apache 跨域,解决nginx/apache静态资源跨域访问问题详解
1. apache静态资源跨域访问 找到apache配置文件httpd.conf 找到这行 #LoadModule headers_module modules/mod_headers.so 把#注释 ...
- nginx配置静态资源,重新发布后,浏览器缓存导致异常原因和解决
原因: nginx作为静态资源服务器,重新构建后发生浏览器页面直接取缓存中的js等文件名,但是由于服务器中文件已经被替换导致出现异常. 浏览器在下次请求这个资源的时候不会将请求发向后端,而是直接从缓存 ...
- 记录使用nginx部署静态资源流程,以及遇到的访问静态资源404问题
nginx部署静态资源 将网站静态资源(HTML,JavaScript,CSS,img等文件)与后台应用分开部署实现动静分离,提高用户访问静态代码的速度,降低对后台应用访问,减轻后台服务器的压力. 将 ...
- Linux环境下Nginx部署静态资源文件。
操作环境: 阿里云服务器: Centos7.4 已安装过nginx 准备好静态资源文件. 部署静态资源文件 我把自己的静态资源文件放在了/usr/local/nginx/html下. dv文件夹中为静 ...
- win10 nginx部署静态资源服务器和HTML
win10 nginx部署前端项目(静态资源服务器和HTML) niginx的安装和启停操作参照博客:https://blog.csdn.net/qq_26666947/article/details ...
最新文章
- Java基础、多线程、JVM、集合八股文自述(持续更新)
- x3650m5不自动进系统_自动起停系统不工作?可能有这几种情况
- 【MySQL】 批量修改数据表和数据表中所有字段的字符集
- 编程的精髓:发现问题,解决问题
- 二叉搜索树的经典问题
- html5shiv.js和respond.min.js
- Perl正则表达式匹配
- LeetCode 1220. 统计元音字母序列的数目(DP)
- oracle pk_serial,Oracle 常用技巧和脚本-数据库专栏,ORACLE
- Unity-TA 成长之路(一)初识渲染管线
- 《孙子兵法》十三篇注译(1--导读)
- #Logback入门 @FDDLC
- java转行失败_转行学JAVA,成功和失败的原因
- 转:Provisioning profile XXXX can't be found
- 刺激战场春节版年兽全网最详细位置,另附刺激战场更新内容
- shell脚本scp自动输入密码
- 琴生不等式(Jensen Inequality)
- OpenLayers基础:在IIS中部署并启用CGI
- 58同城用户行为数仓建设及实践
- 关于bin和obj文件夹
热门文章
- godspeed机器人_机器人总动员?经典 及其它
- 2023年,云计算还有发展前景吗?
- 爬虫厉害?反爬虫技术才是真的牛!
- HDU 6599:I Love Palindrome String Manacher+回文自动机
- vue 操作多维数组
- PF使用率过高的解析以及处理方法
- error: ‘getCurrentCUDAStream’ is not a member of ‘at::cuda’ cudaStream_t stream = at::cuda::getCurre
- Buu:[FlareOn6]Overlong
- 1681_Ubuntu下查看某些文件夹下所有的文件大小
- NLP基础:语言模型