偶尔会用到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