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默认配置方 ...
随机推荐
- JS 07 Dom
DOM(Document Object Model): 结点的概念:整个文档就是由层次不同的多个节点组成,可以说结点代表了全部内容. 结点类型 1.元素结点 2.属性结点 3.文本结点 结点的注意 ...
- pycharm使用pylint
# 与能查到的多数博文做法不太一样,自认为这样更简单有效 1. 下载插件 https://plugins.jetbrains.com/plugin/11084-pylint 2. 从磁盘安装插件 注意 ...
- 虚拟机Vmware使用记录
一直使用的是docker for windows,但是总会出现能打包,能打tag,但是push超时,所以想着弄个虚拟机来实现. 第一步: 安装VMware,安装一个ubantu最新的系统. 第二步: ...
- (十三)Hibernate中的多表操作(3):单向多对多
多对多的处理方式是,有一张中间表,中间表保存两个多方之间的关系.首先来看实际应用场景:在之前开发的系统中,应用了基于角色的控制访问,也就是RBAC模型,一个用户可能存在多种角色,一种角色也可能有多个用 ...
- (三)第一个 Hibernate项目
1.创建java工程,并导入hibernate所需要的jar包 2.通过IDE构建一个基础的Hibernate工程. 产生 hibernate.cfg.xml的框架总配置文件. H ...
- (七)lucene之中文检索和高亮显示以及摘要
前提:本章节使用lucene5.3.0版本,luke也是此版本的. 1.1 生成索引 package com.shyroke.lucene; import java.io.IOException; ...
- (一)SpringMvc简介以及第一个springmvc工程
一.SpringMVC是什么? springmvc是Spring的一个模块,提供web层解决方案(就与MVC设计架构) 如上图, DispatcherServlet:前端控制器,由SpringMVC提 ...
- SQLAlchemy 在查询期间丢失与MySQL服务器的连接
遇到问题 pymysql.err.OperationalError: (2013, 'Lost connection to MySQL server during query') 建立的 pymysq ...
- Java 面向对象_继承
继承 在继承的关系中,子类就是一个父类,也就是说,子类可以被当做父类看待,例如:父类是员工,子类是程序员,那么程序员就是一个员工,代码示例: // 员工类 public class Employee{ ...
- 【小知识点】如何通过jq获取textarea的值
textarea和input本质差不多,只不过input是一行文本,textarea是多行文本. 所以获取textarea的值的方法和获取input的值的方法是一致的, $('textarea').v ...