偶尔会用到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配置及使用的更多相关文章

  1. nginx配置反向代理或跳转出现400问题处理记录

    午休完上班后,同事说测试站点访问接口出现400 Bad Request  Request Header Or Cookie Too Large提示,心想还好是测试服务器出现问题,影响不大,不过也赶紧上 ...

  2. Windos环境用Nginx配置反向代理和负载均衡

    Windos环境用Nginx配置反向代理和负载均衡 引言:在前后端分离架构下,难免会遇到跨域问题.目前的解决方案大致有JSONP,反向代理,CORS这三种方式.JSONP兼容性良好,最大的缺点是只支持 ...

  3. Windows下Nginx配置SSL实现Https访问(包含证书生成)

    Vincent.李   Windows下Nginx配置SSL实现Https访问(包含证书生成) Windows下Nginx配置SSL实现Https访问(包含证书生成) 首先要说明为什么要实现https ...

  4. Nginx 配置简述

    不论是本地开发,还是远程到 Server 开发,还是给提供 demo 给人看效果,我们时常需要对 Nginx 做配置,Nginx 的配置项相当多,如果考虑性能配置起来会比较麻烦.不过,我们往往只是需要 ...

  5. Nginx配置详解

    序言 Nginx是lgor Sysoev为俄罗斯访问量第二的rambler.ru站点设计开发的.从2004年发布至今,凭借开源的力量,已经接近成熟与完善. Nginx功能丰富,可作为HTTP服务器,也 ...

  6. Nginx配置Https

    1.申请证书: https://console.qcloud.com/ssl?utm_source=yingyongbao&utm_medium=ssl&utm_campaign=qc ...

  7. nginx配置为windows服务中的坑

    网上搜索“nginx 配置为windows服务”,很容易搜索到使用windows server warpper来配置,于是按照网上的方法我从github上的链接下载了1.17版本,前面都很顺利,很容易 ...

  8. 【nginx配置】nginx做非80端口转发

    一个场景 最近在使用PHP重写一个使用JAVA写的项目,因为需要查看之前的项目,所以要在本地搭建一个Tomcat来跑JAVA的项目.搭建成功后,因为Tomcat监听的端口是8080,因此,访问的URL ...

  9. Apache、nginx配置的网站127.0.0.1可以正常访问,内外网的ip地址无法访问,谁的锅?

    最近做开发,发现一个比较尴尬的问题.因为我是一个web开发者,经常要用到Apache或者nginx等服务器软件,经过我测试发现,只要我打开了adsafe,我便不能通过ip地址访问我本地的网站了,比如我 ...

  10. nginx配置301重定向

    1. 简介 301重定向可以传递权重,相比其他重定向,只有301是最正式的,不会被搜索引擎判断为作弊 2. 栗子 savokiss.com 301到 savokiss.me 3. nginx默认配置方 ...

随机推荐

  1. SrpingBoot入门到入坟01-HelloWorld和SpringBoot打Jar包

    第一个SpringBoot: 建立一个maven项目: 再pom.xml里面增加依赖: <?xml version="1.0" encoding="UTF-8&qu ...

  2. 通过vs2015给QT添加模块

    Qt VS Tools->Qt Project Settings->Qt Modules

  3. SAS学习笔记29 logistic回归

    变量筛选 当对多个自变量建立logistic回归模型时,并不是每一个自变量对模型都有贡献.通常我们希望所建立的模型将具有统计学意义的自变量都包含在内,而将没有统计学意义的自变量排除在外,即进行变量筛选 ...

  4. shell习题第22题:

    [题目要求] 加入A服务器可直接ssh到B,不用输入密码.A和B都有一个目录是/data/web/这下有很多文件,我们不知道这下面有多少层目录,但是之前的目录结构和文件是一模一样的.但是现在不确定是否 ...

  5. 查找-------(HashCode)哈希表的原理

    这段时间 在 准备软件设计师考试    目的是想复习一下  自己以前没怎么学的知识    在这个过程中  有了很大的收获  对以前不太懂得东西  在复习的过程中  有了很大程度的提高 比如在复习 程序 ...

  6. Asp.Net Core 轻松学系列-3项目目录和文件作用介绍

    目录 前言 结语 前言     上一章介绍了 Asp.Net Core 的前世今生,并创建了一个控制台项目编译并运行成功,本章的内容介绍 .NETCore 的各种常用命令.Asp.Net Core M ...

  7. javascript--HTML DOM常用元素对象

    二,Select:访问select元素 属性:.selectedIndex  获取select中当前选中项的下标 .options  获取select中所有的option元素 返回值为数组 .opti ...

  8. SQL将同样标识的查询结果查重并用逗号拼接

    SELECT B.TaskID , LEFT(SamList, LEN(SamList) - 1) AS ResultListFROM ( SELECT TaskID , ( SELECT Sampl ...

  9. vue路由(一)

    “vue.js 路由允许我们通过不同的 URL 访问不同的内容.通过 Vue.js 可以实现多视图的单页Web应用(single page web application,SPA)”这段是从网上直接抄 ...

  10. C++ 项目和资源导引

    值得学习的C语言开源项目 注意:本文转载自:https://blog.csdn.net/a110658684/article/details/78862348 - 1. Webbench Webben ...