Nginx 安装部署

91 阅读2分钟
sudo apt update
sudo apt upgrade
# 1、安装
sudo apt update
sudo apt install nginx

# 2、安装完,nginx就默认被启动,通过下面命令查看
sudo systemctl status nginx

启动命令
sudo systemctl start nginx
停止命令
sudo systemctl stop nginx

# 3、配置防火墙,允许流量通过 HTTP(80)和 HTTPS(443)端口。假设你正在使用UFW,你可以做的是启用 ‘Nginx Full’ profile,它包含了这两个端口:
sudo ufw allow 'Nginx Full'
sudo ufw status(验证是否成功)

# 4、验证nginx是否安装成功
curl http://127.0.0.1

安装目录为 /etc/nginx

找到CONF

image.png

先备份conf

cp nginx.conf /etc/nginx/nginx.conf.bak

在进行编辑: vi nginx.conf

worker_processes                1;

events {
  worker_connections            10240;
}

http {
  log_format                    '$remote_addr - $remote_user [$time_local] ' '"$request" $status $body_bytes_sent ' '"$http_referer" "$http_user_agent"';
  include                       mime.types;
  default_type                  application/octet-stream;
  sendfile                      on;
  #autoindex                    on;
  #autoindex_exact_size         off;
  autoindex_localtime           on;
  keepalive_timeout             65;
  gzip                          on;
  gzip_disable                  "msie6";
  gzip_min_length               100;
  gzip_buffers                  4 16k;
  gzip_comp_level               1;
  gzip_types                  text/plain application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;
  gzip_types                    "*";
  gzip_vary                     off;
  server_tokens                 off;
  client_max_body_size          200m;

server { # 项目启动端口 listen 80;
# 域名(localhost) server_name _; 
# 禁止 iframe 嵌套 add_header X-Frame-Options SAMEORIGIN;
# 访问地址 根路径配置 
location / { # 项目目录 root html; 
# 默认读取文件 index index.html;
# 配置 history 模式的刷新空白
try_files $uri $uri/ /index.html; 
} 
# 后缀匹配,解决静态资源找不到问题 
location ~* \.(gif|jpg|jpeg|png|css|js|ico)$ { root html/static/; }
# 图片防盗链 
location ~/static/.*\.(jpg|jpeg|png|gif|webp)$ 
{ root html; valid_referers *.deeruby.com;
if ($invalid_referer) { return 403; } }
# 访问限制 
location /static { 
root html; 
# allow 允许
allow 39.xxx.xxx.xxx;
# deny 拒绝 deny all;
} 
}

  include                       ../serve/*.conf;
}

跨域请求


location / {  
    # 允许跨域的请求,可以自定义变量$http_origin,*表示所有  
    add_header 'Access-Control-Allow-Origin' *;  
    # 允许携带cookie请求  
    add_header 'Access-Control-Allow-Credentials' 'true';  
    # 允许跨域请求的方法:GET,POST,OPTIONS,PUT  
    add_header 'Access-Control-Allow-Methods' 'GET,POST,OPTIONS,PUT';  
    # 允许请求时携带的头部信息,*表示所有  
    add_header 'Access-Control-Allow-Headers' *;  
    # 允许发送按段获取资源的请求  
    add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range';  
    # 一定要有!!!否则Post请求无法进行跨域!  
    # 在发送Post跨域请求前,会以Options方式发送预检请求,服务器接受时才会正式请求  
    if ($request_method = 'OPTIONS') {  
        add_header 'Access-Control-Max-Age' 1728000;  
        add_header 'Content-Type' 'text/plain; charset=utf-8';  
        add_header 'Content-Length' 0;  
        # 对于Options方式的请求返回204,表示接受跨域请求  
        return 204;  
    }  
}

443端口配置

# ----------HTTPS配置-----------  
server {  
    # 监听HTTPS默认的443端口  
    listen 443;  
    # 配置自己项目的域名  
    server_name www.xxx.com;  
    # 打开SSL加密传输  
    ssl on;  
    # 输入域名后,首页文件所在的目录  
    root html;  
    # 配置首页的文件名  
    index index.html index.htm index.jsp index.ftl;  
    # 配置自己下载的数字证书  
    ssl_certificate  certificate/xxx.pem;  
    # 配置自己下载的服务器私钥  
    ssl_certificate_key certificate/xxx.key;  
    # 停止通信时,加密会话的有效期,在该时间段内不需要重新交换密钥  
    ssl_session_timeout 5m;  
    # TLS握手时,服务器采用的密码套件  
    ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;  
    # 服务器支持的TLS版本  
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;  
    # 开启由服务器决定采用的密码套件  
    ssl_prefer_server_ciphers on;  
  
    location / {  
        ....  
    }  
}  
  
# ---------HTTP请求转HTTPS-------------  
server {  
    # 监听HTTP默认的80端口  
    listen 80;  
    # 如果80端口出现访问该域名的请求  
    server_name www.xxx.com;  
    # 将请求改写为HTTPS(这里写你配置了HTTPS的域名)  
    rewrite ^(.*)$ https://www.xxx.com;  
}