焦点资讯:Nginx 常用的基础配置(web前端相关方面)
(相关资料图)
最近很多朋友通过趣站网问到Nginx配置前端 web 服务,所以特地写了这篇文章;希望能够帮助更多的朋友。
基础配置
user root;worker_processes 1;events { worker_connections 10240;}http { log_format "$remote_addr - $remote_user [$time_local] " ""$request" $status $body_bytes_sent " ""$http_referer" "$http_user_agent""; include mime.types; default_type application/octet-stream; sendfile on; #autoindex on; #autoindex_exact_size off; autoindex_localtime on; keepalive_timeout 65; gzip on; gzip_disable "msie6"; gzip_min_length 100; gzip_buffers 4 16k; gzip_comp_level 1; gzip_types text/plain application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png; gzip_types "*"; gzip_vary off; server_tokens off; client_max_body_size 200m; server { listen 80 default_server; server_name _; return 403 /www/403/index.html; } include ../serve/*.conf;}复制代码
隐藏 Nginx 版本信息
http { server_tokens off;}复制代码
禁止ip直接访问80端口
server { listen 80 default; server_name _; return 500;}复制代码
启动 web 服务 (vue 项目为例)
server { # 项目启动端口 listen 80; # 域名(localhost) server_name _; # 禁止 iframe 嵌套 add_header X-Frame-Options SAMEORIGIN; # 访问地址 根路径配置 location / { # 项目目录 root html; # 默认读取文件 index index.html; # 配置 history 模式的刷新空白 try_files $uri $uri/ /index.html; } # 后缀匹配,解决静态资源找不到问题 location ~* \.(gif|jpg|jpeg|png|css|js|ico)$ { root html/static/; } # 图片防盗链 location ~/static/.*\.(jpg|jpeg|png|gif|webp)$ { root html; valid_referers *.deeruby.com; if ($invalid_referer) { return 403; } } # 访问限制 location /static { root html; # allow 允许 allow 39.xxx.xxx.xxx; # deny 拒绝 deny all; }}复制代码
PC端和移动端使用不同的项目文件映射
server { ...... location / { root /home/static/pc; if ($http_user_agent ~* "(mobile|android|iphone|ipod|phone)") { root /home/static/mobile; } index index.html; }}复制代码
一个web服务,配置多个项目 (location 匹配路由区别)
server { listen 80; server_name _; # 主应用 location / { root html/main; index index.html; try_files $uri $uri/ /index.html; } # 子应用一 location ^~ /store/ { proxy_pass http://localhost:8001; proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } # 子应用二 location ^~ /school/ { proxy_pass http://localhost:8002; proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } # 静态资源读取不到问题处理 rewrite ^/api/profile/(.*)$ /(替换成正确路径的文件的上一层目录)/$1 last;}# 子应用一服务server { listen 8001; server_name _; location / { root html/store; index index.html; try_files $uri $uri/ /index.html; } location ^~ /store/ { alias html/store/; index index.html index.htm; try_files $uri /store/index.html; } # 接口代理 location /api { proxy_pass http://localhost:8089; }}# 子应用二服务server { listen 8002; server_name _; location / { root html/school; index index.html; try_files $uri $uri/ /index.html; } location ^~ /school/ { alias html/school/; index index.html index.htm; try_files $uri /school/index.html; } # 接口代理 location /api { proxy_pass http://localhost:10010; }}复制代码
配置负载均衡
upstream my_upstream { server http://localhost:9001; server http://localhost:9002; server http://localhost:9003;}server { listen 9000; server_name test.com; location / { proxy_pass my_upstream; proxy_set_header Host $proxy_host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; }}复制代码
SSL 配置 HTTPS
server { listen 80; server_name www.xxx.com; # 将 http 重定向转移到 https return 301 https://$server_name$request_uri;}server { listen 443 ssl; server_name www.xxx.com; ssl_certificate /etc/nginx/ssl/www.xxx.com.pem; ssl_certificate_key /etc/nginx/ssl/www.xxx.com.key; ssl_session_timeout 10m; ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4; ssl_protocols TLSv1 TLSv1.1 TLSv1.2; ssl_prefer_server_ciphers on; location / { root /project/xxx; index index.html index.htm index.md; try_files $uri $uri/ /index.html; }}
关键词: Nginx
责任编辑:孙知兵
免责声明:本文仅代表作者个人观点,与太平洋财富网无关。其原创性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容、文字的真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
如有问题,请联系我们!
- 天天快资讯:银保监会:2022年末小微企业贷2023-02-16
- 世界快消息!山东开元电子有限公司2023-02-16
- 环球最资讯丨【读财报】三季度保险公司消费2023-02-16
- 焦点关注:通策医疗终止收购和仁科技股份 2023-02-16
- 上市航司1月份经营实现开门红 “航空超级2023-02-16
- 天天微头条丨ST天业:杨超诉公司证券虚假陈2023-02-16
- 【天天快播报】股票行情在线看 A股收市432023-02-16
- 如何买股票新手入门?|如何在最佳时机买入2023-02-16
- 【世界独家】防己科是什么?本科的粉防己、2023-02-16
- 世界球精选!最新!央行调整职工住房公积金2023-02-16
- 全球今头条!广西桂花树有什么特征?广西桂2023-02-16
- 商业城半年报:净利大增2.2亿 转让盛京银2023-02-16
- 环球即时:仙丹花怎么养?仙丹花的形态特征2023-02-16
- 沪深300指数基金推荐:132支中证500基金(上)2023-02-16
- 天天观点:农村黄瓜的主要病虫害是什么?如2023-02-16
- 世界信息:首套房房贷利率下调至4.25% 楼市2023-02-16
- 【环球快播报】为什么要炸北溪管道?北溪22023-02-16
- 当前速看:雪美人的栽培技术你知道吗?雪美2023-02-16
- 环球即时看!环球黑卡究竟是什么?持卡人可2023-02-16
- 【天天报资讯】怎么和银行谈停息挂账?停息2023-02-16
- 全球报道:腰果的价格区间是多少?如何确保2023-02-16
- 【世界聚看点】海富通基金管理有限公司:102023-02-16
- 每日速递:宋引章喜欢顾千帆吗?最后的结局2023-02-16
- 环球微速讯:蚜虫怎么治疗?三角梅杀死蚜虫2023-02-16
- 京东金融推出“8.8%”理财产品 基金面值将2023-02-16
- 股票分红为何要除权?配股是否需要除权除息2023-02-16
- 观察:千针万线草是什么?千针万线草有什么2023-02-16
- 实时:互联网金融龙头股票 五只低估值极具2023-02-16
- 豌豆苗的种植方法 种植豌豆苗要注意的几种2023-02-16
- 富士康遭黑客勒索比特币1804枚 价格高达2.2023-02-16
精彩推荐
- 天天快资讯:银保监会:2022年末小微企...
- 世界快消息!山东开元电子有限公司
- 【环球快播报】为什么要炸北溪管道?北...
- 【天天报资讯】怎么和银行谈停息挂账?...
- 每日速递:宋引章喜欢顾千帆吗?最后的...
- 股票分红为何要除权?配股是否需要除权...
- 富士康遭黑客勒索比特币1804枚 价格高...
- 世界快讯:刘晓庆坐牢原因是什么?刘晓庆...
- 环球观速讯丨佟丽娅是顺产还是剖腹产?...
- 要闻速递:经历萨德风波后 韩国乐天超...
- 聚焦:景甜是景俊海女儿?网传景甜父亲...
- 张震为什么不娶舒淇?张震与舒淇的前尘往事
- 焦点速讯:杨幂胡歌翻脸原因 杨幂胡歌...
- 博纳影业总裁于冬身价多少亿?金巧巧结...
- 英国留学生包机回国费用公布:经济舱票...