nginx配置及使用
偶尔会用到nginx部署项目,记录nginx配置备忘。主要有端口、地址及别名,代理转发和https配置。
配置文件为nginx.conf。
部署http项目:
1.找到http下的server配置项
端口和servername配置,即访问地址中http://localhost:9003
listen 9003;
server_name localhost;
2.配置项目
项目结构如下

nginx增加location配置项:
location /dist {
root E:/code/02project/rt-poc; # root 时 root路径+location路径映射到服务器文件
index index.html;
}
访问地址为:http://localhost:9003/dist/index.html
# alias 时 location代替root路径映射到服务器文件上
location /webapp {
alias E:/code/02project/rt-poc/dist;
index index.html;
}
访问地址为:http://localhost:9003/webapp/index.html
可以浏览文件
#autoindex 开启后可以显示目录
location /images {
root E:/code/00test/picture;
autoindex on;
}
# 代理转发
location /my-web {
proxy_pass http://localhost:9003/dist;
}
访问 http://localhost:9003/my-web/index.html 即跳转为 http://localhost:9003/dist/index.html
部署https项目:
1.找到https下的server配置项,设置默认的443端口及ssl证书地址,location配置和http一样,这里也可以转发http下的server,使其能以https的方式访问
listen 443 ssl;
server_name localhost; ssl_certificate E:/code/03server/nginx-1.16.0/ssl/nginx_https.crt;
ssl_certificate_key E:/code/03server/nginx-1.16.0/ssl/nginx_https.key;
转发http服务
# 转发http服务,变为https
location /tour {
proxy_pass http://localhost:9003/webapp;
}
nginx相关命令:
// window下 进入nginx目录,启动cmd
start nginx -- 启动
nginx -s reload -- 重载配置
nginx -s quit --退出
nginx -s stop
nginx.conf文件完整内容,可参考进行配置。
#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"'; #access_log logs/access.log main; sendfile on;
#tcp_nopush on; #keepalive_timeout 0;
keepalive_timeout 65; #gzip on; server {
listen 9003;
server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; # location / {
# root html;
# index index.html;
# } #autoindex 开启后可以显示目录
location /images {
root E:/code/00test/picture;
autoindex on;
} # alias 时 location代替root路径映射到服务器文件上
location /tour {
alias E:/code/02project/ni-tour/ni-tour-web/dist;
index index.html;
} # root 时 root路径+location路径映射到服务器文件上
location /dist {
root E:/code/02project/smrt-poc/smrt-poc-tbox-html;
index index.html;
} # 代理转发
location /smrt {
proxy_pass http://localhost:9003/dist;
} location / {
root E:/code/00test/picture;
autoindex on;
} #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;
#}
} # another virtual host using mix of IP-, name-, and port-based configuration
# 可以监听不同的端口
server {
listen 9005;
server_name localhost; location /smrt {
alias E:/code/02project/smrt-poc/smrt-poc-tbox-html/dist;
index index.html;
}
} #HTTPS server
server {
listen 443 ssl;
server_name localhost; ssl_certificate E:/code/03server/nginx-1.16.0/ssl/nginx_https.crt;
ssl_certificate_key E:/code/03server/nginx-1.16.0/ssl/nginx_https.key; ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m; ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on; location /images {
root E:/code/00test/picture;
autoindex on;
} location /smrt {
alias E:/code/02project/smrt-poc/smrt-poc-tbox-html/dist;
index index.html;
} # 转发http服务,变为https
location /tour {
proxy_pass http://localhost:9003/tour;
}
} }
配置https需要的ssl证书地址 可参考https://blog.csdn.net/sunroyfcb/article/details/83592553 缓存配置
server前添加如下配置项:
proxy_cache_path /opt/map-web-model/map-web-buildings-cache levels=: keys_zone=buildingscache:10m max_size=10g inactive=300m use_temp_path=off;
相关配置说明如下:
- /path/to/cache 本地路径,用来设置Nginx缓存资源的存放地址
- levels 默认所有缓存文件都放在同一个/path/to/cache下,但是会影响缓存的性能,因此通常会在/path/to/cache下面建立子目录用来分别存放不同的文件。假设levels=1:2,Nginx为将要缓存的资源生成的key为f4cd0fbc769e94925ec5540b6a4136d0,那么key的最后一位0,以及倒数第2-3位6d作为两级的子目录,也就是该资源最终会被缓存到/path/to/cache/0/6d目录中
- key_zone 在共享内存中设置一块存储区域来存放缓存的key和metadata(类似使用次数),这样nginx可以快速判断一个request是否命中或者未命中缓存,1m可以存储8000个key,10m可以存储80000个key
- max_size 最大cache空间,如果不指定,会使用掉所有disk space,当达到配额后,会删除最少使用的cache文件
- inactive 未被访问文件在缓存中保留时间,本配置中如果60分钟未被访问则不论状态是否为expired,缓存控制程序会删掉文件。inactive默认是10分钟。需要注意的是,inactive和expired配置项的含义是不同的,expired只是缓存过期,但不会被删除,inactive是删除指定时间内未被访问的缓存文件
- use_temp_path 如果为off,则nginx会将缓存文件直接写入指定的cache文件中,而不是使用temp_path存储,official建议为off,避免文件在不同文件系统中不必要的拷贝
- proxy_cache 启用proxy cache,并指定key_zone。另外,如果proxy_cache off表示关闭掉缓存。
使用:
location /buildings {
proxy_cache buildingscache;
alias /opt/map-web-model/buildings;
autoindex off;
}
常见错误
1.访问服务出现以下错误:
The 'Access-Control-Allow-Origin' header contains multiple values '*, *', but only one is allowed.
这种问题常见于nginx做代理,设置了add_header 'Access-Control-Allow-Origin' '*';
而被代理的服务本身已经设置了'Access-Control-Allow-Origin' '*'; 会出现了header重复,删掉代理的即可。
nginx配置及使用的更多相关文章
- nginx配置反向代理或跳转出现400问题处理记录
午休完上班后,同事说测试站点访问接口出现400 Bad Request Request Header Or Cookie Too Large提示,心想还好是测试服务器出现问题,影响不大,不过也赶紧上 ...
- Windos环境用Nginx配置反向代理和负载均衡
Windos环境用Nginx配置反向代理和负载均衡 引言:在前后端分离架构下,难免会遇到跨域问题.目前的解决方案大致有JSONP,反向代理,CORS这三种方式.JSONP兼容性良好,最大的缺点是只支持 ...
- Windows下Nginx配置SSL实现Https访问(包含证书生成)
Vincent.李 Windows下Nginx配置SSL实现Https访问(包含证书生成) Windows下Nginx配置SSL实现Https访问(包含证书生成) 首先要说明为什么要实现https ...
- Nginx 配置简述
不论是本地开发,还是远程到 Server 开发,还是给提供 demo 给人看效果,我们时常需要对 Nginx 做配置,Nginx 的配置项相当多,如果考虑性能配置起来会比较麻烦.不过,我们往往只是需要 ...
- Nginx配置详解
序言 Nginx是lgor Sysoev为俄罗斯访问量第二的rambler.ru站点设计开发的.从2004年发布至今,凭借开源的力量,已经接近成熟与完善. Nginx功能丰富,可作为HTTP服务器,也 ...
- Nginx配置Https
1.申请证书: https://console.qcloud.com/ssl?utm_source=yingyongbao&utm_medium=ssl&utm_campaign=qc ...
- nginx配置为windows服务中的坑
网上搜索“nginx 配置为windows服务”,很容易搜索到使用windows server warpper来配置,于是按照网上的方法我从github上的链接下载了1.17版本,前面都很顺利,很容易 ...
- 【nginx配置】nginx做非80端口转发
一个场景 最近在使用PHP重写一个使用JAVA写的项目,因为需要查看之前的项目,所以要在本地搭建一个Tomcat来跑JAVA的项目.搭建成功后,因为Tomcat监听的端口是8080,因此,访问的URL ...
- Apache、nginx配置的网站127.0.0.1可以正常访问,内外网的ip地址无法访问,谁的锅?
最近做开发,发现一个比较尴尬的问题.因为我是一个web开发者,经常要用到Apache或者nginx等服务器软件,经过我测试发现,只要我打开了adsafe,我便不能通过ip地址访问我本地的网站了,比如我 ...
- nginx配置301重定向
1. 简介 301重定向可以传递权重,相比其他重定向,只有301是最正式的,不会被搜索引擎判断为作弊 2. 栗子 savokiss.com 301到 savokiss.me 3. nginx默认配置方 ...
随机推荐
- NotePad++ 正则表达式 转
https://gerardnico.com/ide/notepad/replace https://notepad-plus-plus.org/community/topic/16787/find- ...
- Bean的三种实例化方式
在面向对象程序中,如要使用某个对象,就需要先实例化这个对象.同样的,在Spring中,要想使用容器中的Bean,也需要实例化Bean.实例化Bean有三种方式,分别是:构造器实例化.静态工厂实例化.实 ...
- python学习-19 字典
字典dict 1.dic = {key:value,key:value} 字典有{ }括住,字典的value可以是任意值,字典的key的值不包括列表和字典 di = {"age": ...
- asp.net core-10.Http请求的处理过程
左边是一个普通的网页请求过程,右边是.net core请求过程 这是大致请求流程图:
- sql 计算奇数还是偶数
& 运算符来判断奇数还是偶数 sql判断奇数还是偶数 3&1 返回 1 2&1 返回0 0&1 返回 0
- Unity插件研究-EasyTouch V5
抽空研究了下Easy Touch 5插件,发现确实很好用,下面是相应的用法: 1. Easy Touch Controls:实现虚拟摇杆的组件 在项目的"Hierarchy"窗口下 ...
- (二十四)JSP标签之基本标签(<jsp:标签名>)
一.常用标签 1.1 jsp中标签一共有8中,其中常用的有6中,本文将介绍这6种常用的标签. 1.2 6种标签 1. <jsp:include> <jsp:include>标签 ...
- Spring 后台方法 重定向 与 转发
一.重定向:重定向是客户端行为,在使用时,务必使用全路径,否则可能因为外部环境导致错误 1.URL改变为重定向的URL地址 2.前台页面不能使用Ajax请求提交, 应该使用form表单提交 方法一.参 ...
- 网络知识(1)TCP/IP五层结构
图1 数据流向图 1,网络基础 1.1 发展 古代:①烽火狼烟最为原始的0-1单bit信息传递:②飞鸽传书.驰道快马通信,多字节通信: 近代:①轮船信号灯:②无线电报[摩尔斯码]: 现代:①有线模拟通 ...
- ASP.NET MVC或者.net Core mvc 页面使用富文本控件的 保存问题
https://blog.csdn.net/leftfist/article/details/69629394 目前在做的项目存在XSS安全漏洞! 原因是有一些页面使用了富文本编辑框,为了使得其内容可 ...