beego项目部署到nginx(含http转https)
之前的程序部署到服务器采用的直接部署,比较方便,现在把它部署到nginx,以便后续的反向代理和负载均衡,同时,因为要接入微信小程序,所以需要将之前的域名http://www.xxxxxx.com转成https://www.xxxxxx.com。
更多内容可关注公众号,里面都是作者原创。
1,安装Nginx。
卸载原来的nginx:sudo apt-get —purge autoremove nginx
安装nginx:sudo apt-get install nginx
如图:
2,部署beego到nginx,增加ssl证书(证书的申请可以到腾讯云注册账号申请,现在有免费一年的证书使用)。
修改/etc/nginx/nginx.conf。也可以在/etc/nginx/conf.d/目录下新增conf文件来配置专属于某个服务。此处配置的是/etc/nginx/nginx.conf。
user www-data; worker_processes auto; pid /run/nginx.pid; events { worker_connections 768; # multi_accept on; } http { ## # Basic Settings ## sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout 65; types_hash_max_size 2048; # server_tokens off; # server_names_hash_bucket_size 64; # server_name_in_redirect off; include /etc/nginx/mime.types; default_type application/octet-stream; ## # SSL Settings ## ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE ssl_prefer_server_ciphers on; ## # Logging Settings ## access_log /var/log/nginx/access.log; error_log /var/log/nginx/error.log; ## # Gzip Settings ## gzip on; gzip_disable "msie6"; # gzip_vary on; # gzip_proxied any; # gzip_comp_level 6; # gzip_buffers 16 8k; # gzip_http_version 1.1; # gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript; ## # Virtual Host Configs ## include /etc/nginx/conf.d/*.conf; include /etc/nginx/sites-enabled/*; server { listen 80; server_name www.XXXXXX.com; #你的域名 # 配置https ssl on; ssl_certificate 1_XXXXXX.com_bundle.crt; #你申请的nginx中的ssl证书 ssl_certificate_key 2_XXXXXX.com.key; #你申请的nginx中的key ssl_session_timeout 5m; ssl_protocols TLSv1 TLSv1.1 TLSv1.2; ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE; ssl_prefer_server_ciphers on; charset utf-8; access_log /home/ubuntu/beego/access.log; #该目录建议是你的部署文件目录 location /(css|js|fonts|img)/ { access_log off; expires 1d; root "/home/ubuntu/beego/static";#该文件是你beego项目中的static静态文件路径 try_files $uri @backend; } location / { try_files /_not_exists_ @backend; } location @backend { proxy_set_header X-Forwarded-For $remote_addr; proxy_set_header Host $http_host; proxy_pass http://127.0.0.1:8999; #代理指向的beego项目地址 } } } #mail { # # See sample authentication script at: # # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript # # # auth_http localhost/auth.php; # # pop3_capabilities "TOP" "USER"; # # imap_capabilities "IMAP4rev1" "UIDPLUS"; # # server { # listen localhost:110; # protocol pop3; # proxy on; # } # # server { # listen localhost:143; # protocol imap; # proxy on; # } #}
加载配置:sudo nginx -s reload
启动nginx:sudo service nginx restart
4,启动beego服务。验证。
启动nginx:sudo service nginx restart
4,启动beego服务。验证。
此时已经将80代理到beego项目的8999端口。