偶尔会用到nginx部署项目,记录nginx配置备忘。主要有端口、地址及别名,代理转发和https配置。
配置文件为nginx.conf。
部署http项目:
1.找到http下的server配置项
端口和servername配置,即访问地址中http://localhost:9003
1 listen 9003; 2 server_name localhost;
2.配置项目
项目结构如下
nginx增加location配置项:
1 location /dist { 2 root E:/code/02project/rt-poc; # root 时 root路径+location路径映射到服务器文件 3 index index.html; 4 }
访问地址为:http://localhost:9003/dist/index.html
1 # alias 时 location代替root路径映射到服务器文件上 2 location /webapp { 3 alias E:/code/02project/rt-poc/dist; 4 index index.html; 5 }
访问地址为:http://localhost:9003/webapp/index.html
可以浏览文件
1 #autoindex 开启后可以显示目录 2 location /images { 3 root E:/code/00test/picture; 4 autoindex on; 5 }
代理转发
1 # 代理转发 2 location /my-web { 3 proxy_pass http://localhost:9003/dist; 4 }
访问 http://localhost:9003/my-web/index.html 即跳转为 http://localhost:9003/dist/index.html
部署https项目:
1.找到https下的server配置项,设置默认的443端口及ssl证书地址,location配置和http一样,这里也可以转发http下的server,使其能以https的方式访问
1 listen 443 ssl; 2 server_name localhost; 3 4 ssl_certificate E:/code/03server/nginx-1.16.0/ssl/nginx_https.crt; 5 ssl_certificate_key E:/code/03server/nginx-1.16.0/ssl/nginx_https.key;
转发http服务
1 # 转发http服务,变为https 2 location /tour { 3 proxy_pass http://localhost:9003/webapp; 4 }
nginx相关命令:
1 // window下 进入nginx目录,启动cmd 2 start nginx -- 启动 3 nginx -s reload -- 重载配置 4 nginx -s quit --退出 5 nginx -s stop
nginx.conf文件完整内容,可参考进行配置。
1 #user nobody; 2 worker_processes 1; 3 4 #error_log logs/error.log; 5 #error_log logs/error.log notice; 6 #error_log logs/error.log info; 7 8 #pid logs/nginx.pid; 9 10 11 events { 12 worker_connections 1024; 13 } 14 15 16 http { 17 include mime.types; 18 default_type application/octet-stream; 19 20 #log_format main '$remote_addr - $remote_user [$time_local] "$request" ' 21 # '$status $body_bytes_sent "$http_referer" ' 22 # '"$http_user_agent" "$http_x_forwarded_for"'; 23 24 #access_log logs/access.log main; 25 26 sendfile on; 27 #tcp_nopush on; 28 29 #keepalive_timeout 0; 30 keepalive_timeout 65; 31 32 #gzip on; 33 34 server { 35 listen 9003; 36 server_name localhost; 37 38 #charset koi8-r; 39 40 #access_log logs/host.access.log main; 41 42 # location / { 43 # root html; 44 # index index.html; 45 # } 46 47 #autoindex 开启后可以显示目录 48 location /images { 49 root E:/code/00test/picture; 50 autoindex on; 51 } 52 53 # alias 时 location代替root路径映射到服务器文件上 54 location /tour { 55 alias E:/code/02project/ni-tour/ni-tour-web/dist; 56 index index.html; 57 } 58 59 # root 时 root路径+location路径映射到服务器文件上 60 location /dist { 61 root E:/code/02project/smrt-poc/smrt-poc-tbox-html; 62 index index.html; 63 } 64 65 # 代理转发 66 location /smrt { 67 proxy_pass http://localhost:9003/dist; 68 } 69 70 location / { 71 root E:/code/00test/picture; 72 autoindex on; 73 } 74 75 #error_page 404 /404.html; 76 77 # redirect server error pages to the static page /50x.html 78 # 79 error_page 500 502 503 504 /50x.html; 80 location = /50x.html { 81 root html; 82 } 83 84 # proxy the PHP scripts to Apache listening on 127.0.0.1:80 85 # 86 #location ~ .php$ { 87 # proxy_pass http://127.0.0.1; 88 #} 89 90 # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 91 # 92 #location ~ .php$ { 93 # root html; 94 # fastcgi_pass 127.0.0.1:9000; 95 # fastcgi_index index.php; 96 # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; 97 # include fastcgi_params; 98 #} 99 100 # deny access to .htaccess files, if Apache's document root 101 # concurs with nginx's one 102 # 103 #location ~ /.ht { 104 # deny all; 105 #} 106 } 107 108 109 # another virtual host using mix of IP-, name-, and port-based configuration 110 # 可以监听不同的端口 111 server { 112 listen 9005; 113 server_name localhost; 114 115 location /smrt { 116 alias E:/code/02project/smrt-poc/smrt-poc-tbox-html/dist; 117 index index.html; 118 } 119 } 120 121 #HTTPS server 122 server { 123 listen 443 ssl; 124 server_name localhost; 125 126 ssl_certificate E:/code/03server/nginx-1.16.0/ssl/nginx_https.crt; 127 ssl_certificate_key E:/code/03server/nginx-1.16.0/ssl/nginx_https.key; 128 129 ssl_session_cache shared:SSL:1m; 130 ssl_session_timeout 5m; 131 132 ssl_ciphers HIGH:!aNULL:!MD5; 133 ssl_prefer_server_ciphers on; 134 135 location /images { 136 root E:/code/00test/picture; 137 autoindex on; 138 } 139 140 location /smrt { 141 alias E:/code/02project/smrt-poc/smrt-poc-tbox-html/dist; 142 index index.html; 143 } 144 145 # 转发http服务,变为https 146 location /tour { 147 proxy_pass http://localhost:9003/tour; 148 } 149 } 150 151 }
配置https需要的ssl证书地址 可参考https://blog.csdn.net/sunroyfcb/article/details/83592553
缓存配置
server前添加如下配置项:
1 proxy_cache_path /opt/map-web-model/map-web-buildings-cache levels=1:2 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表示关闭掉缓存。
使用:
1 location /buildings { 2 proxy_cache buildingscache;
3 alias /opt/map-web-model/buildings;
4 autoindex off;
5 }
常见错误
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重复,删掉代理的即可。