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端口。
