介绍
- 高性能的web服务器,开源免费
- 一般用于做静态服务,负载均衡
- 用于反向代理
安装
Mac使用homeBrow安装,Centos使用yum安装
常用命令
- # 重启nginx
- nginx -s reload
- # 停止nginx
- nginx -s stop
- # 测试配置文件是否正确,同时会列出配置文件位置
- nginx -t
Nginx配置
- # 使用 nginx -t 来显示conf文件位置
- http{
- server{
- listen 8080; # 监听端口
- server_name localhost; # _相当于0.0.0.0;也可以指定域名
- # 配置静态文件
- location / {
- root /User/me/html; # /路由寻找的目录
- index index.html; # index文件名
- }
- # 配置动态转发
- location /home { # 转发
- proxy_pass http://localhost:8001;
- }
- location /api/ { # 转发
- proxy_pass http://localhost:8000;
- proxy_set_header Host $host;
- }
- }
- }
快速启动动态服务
用途:
- 和前端联调时,登录功能依赖cookie,必须使用浏览器联调
- cookie跨域不共享,前端和server必须同域
- 需要用nignx做代理,让前后端同域
使用node的http服务
- # 需要sudo权限;-g是global全局安装
- npm install http-server -g
- # 启动http服务,指定端口,默认是8080
- http-server -p 8001
云服务器配置https
- 申请腾讯云证书
- 下载并上传到服务器,解压:目录中有Apache、Nginx、Tomcat等文件夹,使用Nginx文件夹中的两个文件,crt文件是证书,key文件是秘钥
- 配置conf:conf文件里有http部分和https部分,默认是打开http部分的,现在注释掉http部分,打开https部分,并指定crt和key文件以及域名即可
- 浏览器访问https://<host> 即可
- server {
- listen 443 ssl http2 default_server;
- listen [::]:443 ssl http2 default_server;
- server_name xxx.xx yyy.yy; # 指定申请的域名,若多个域名使用空格隔开
- root /usr/share/nginx/html;
-
- ssl_certificate "/etc/pki/nginx/server.crt”; # 复制crt文件
- ssl_certificate_key "/etc/pki/nginx/private/server.key”; # 复制key文件
- ssl_session_cache shared:SSL:1m;
- ssl_session_timeout 10m;
- ssl_ciphers PROFILE=SYSTEM;
- ssl_prefer_server_ciphers on;
-
- # Load configuration files for the default server block.
- include /etc/nginx/default.d/*.conf;
-
- location / {
- }
-
- error_page 404 /404.html;
- location = /40x.html {
- }
-
- error_page 500 502 503 504 /50x.html;
- location = /50x.html {
- }
- }