宋子宪博客

Nginx配置Spring Boot项目开启域名访问详细教程

不开启HTTPS配置

创建一个新的nginx文件

    upstream szxblog{
     server 127.0.0.1:8088; #此处的ip写服务器的真实ip,因为是docker构建的,不然可能访问不到
     server 127.0.0.1:8088 backup; #备机
    }

 # 这里配置的是80端口监听job.songzixian.com域名
    server {
        listen       80;
        server_name  job.songzixian.com;
 
    location / {
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_connect_timeout   10s;
        proxy_send_timeout      60s;
        proxy_read_timeout      60s;
        proxy_ignore_client_abort   on;
        proxy_pass http://szxblog/; #此处与上面的upstream处对应
    }
    
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   html;
    }
 }

开启HTTPS配置 配置SSL开启HTTPS域名访问

创建一个新的nginx文件

# 负载均衡-->该节点下可配置多个server,按时间节点轮流分配
upstream szxblog{
 server 127.0.0.1:8088; #此处的ip写服务器的真实ip,因为是docker构建的,不然可能访问不到
 server 127.0.0.1:8088 backup; #备机
}

server {
    listen 443 ssl;  
    server_name job.songzixian.com; 

    #- 需要将job.songzixian.com.pem替换成已上传的证书文件的名称。     
    ssl_certificate /etc/nginx/cert/job.songzixian.com.pem;
    #- 需要将job.songzixian.com.key替换已上传的证书密钥文件的名称。     
    ssl_certificate_key /etc/nginx/cert/job.songzixian.com.key;    
    # - 客户端可以重用会话缓存中ssl参数的过期时间
    ssl_session_timeout 5m;
    # -加密
    ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
    ssl_prefer_server_ciphers on;
    # 协议
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3;

    ssl_session_cache shared:SSL:1m;
     
    fastcgi_param  HTTPS        yes; # - no关闭
    fastcgi_param  HTTP_SCHEME  https;
    
    location / {
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_connect_timeout   10s;
        proxy_send_timeout      60s;
        proxy_read_timeout      60s;
        proxy_ignore_client_abort   on;
        proxy_pass http://szxblog/; #此处与上面的upstream处对应
    }
    
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   html;
    }
    
}

#- 设置HTTP请求自动强制转HTTPS
server {
    listen 80;
    #- job.songzixian.com替换成自己的域名
    server_name job.songzixian.com;
    rewrite ^(.*)$ https://$host$1;
    location /{}
}

加载自定义nginx文件
通过include命令,加载自定义nginx配置文件

Nginx命令

启动nginx

/usr/local/nginx/sbin/nginx

查看nginx进程

ps -aux | grep 'nginx'

停止nginx

/usr/local/nginx/sbin/nginx -s stop

重启nginx

/usr/local/nginx/sbin/nginx -s reopen

当前页面是本站的「Google AMP」版。查看和发表评论请点击:完整版 »