如题,今儿个,假设手头上,已经有了,阿里云上的申请域名、安全组配置和ssl证书密钥(没有的去阿里云控制台申请即可),并且通过ip+端口号能访问服务器上跑的项目,所以接下来需要把此服务器上ip+端口跑的项目与申请好的域名相绑定,分为以下几个大步骤:
1.linux下安装nginx
2.修改nginx里面的配置文件,配置nginx代理你服务器里面的项目
- 使用工具:文件传输工具:winSCP,服务器:xshell6,开发工具:idea
那么,开始吧:
第一大步:linux下安装nginx:
- 这里是用的yum命令(yum是linux软件包管理器,首先确保你的linux有yum)快捷下载nginx
- (1)添加源:默认centos-linux无nginx的源,但nginx提供了centos的源地址,执行下面命令添加nginx源:
sudo rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
- (2)安装nginx:
sudo yum install -y nginx
- (3)启动nginx,设置开机自动运行:
sudo systemctl start nginx.service
sudo systemctl enable nginx.service
- (4)查看nginx是否运行成功:
[root@yankerp ~]# netstat -anput | grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 27228/nginx: master
此命令查看是否有nginx网络程序服务。
第二大步:配置nginx代理你服务器里面的项目:
1.准备好你的阿里云服务器和一个可用可连接外网的域名
2.在阿里云上申请ssl证书,并为这个域名配置证书,这个证书有很多web服务器的版本,下载nginx版本的ssl证书。
3.查看你的nginx安装目录,通过查看版本命令:
nginx -v
这个命令会输出很多信息,如下图:
其中configure arguments: --prefix=/etc/nginx
,这个prefix后就是你的nginx安装目录,cd到这个/etc/nginx目录,在里面建一个目录cert,具体为/etc/nginx/cert,用来存放你的ssl证书文件,说到这个ssl证书文件,就相当于证书密钥,一个pem一个key文件,把这两个关于ssl的文件通过winSCP工具从Windows电脑粘贴到你的这个cert目录。
4.配置你的nginx.config文件
cd到你的nginx目录下,我的是这样的:编辑nginx.config文件,
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
}
一般情况下,这个文件会包括http{ }、server{ }等模块,这里用命令包含文件的方法,把/etc/nginx/conf.d下的所有后缀为.config的配置文件包含进来解耦了,所以,cd到/etc/nginx/conf.d目录下,我这里有一个默认配置文件,打开它:
server {
listen 80; #作用:监听http,listen 443 监听https。
server_name laopo520.com; #填写域名
root html;
index index.html index.htm;
listen 443 ssl; #旧版本的是ssl on; 命令,但是nginx新版本不支持,需要这样改动
ssl_certificate cert/1965355_www.laopo520.com.pem; #你的pem文件目录
ssl_certificate_key cert/1965355_www.laopo520.com.key; # 你的key文件目录
ssl_session_timeout 5m;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://127.0.0.1:8766/; #代理你的服务器本地项目
proxy_redirect off;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
几个关键点我都用注释标注了,改动之后,我们需要执行命令:/usr/sbin/nginx -t测试一下配置文件是否有错误,错误的时候会有行数提示,很方便。
[root@iz2ze2fbqxna6qqacoex41z /]# /usr/sbin/nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
修改配置文件成功之后,我们重启nginx服务,然后查看关于nginx的网络服务:
[root@iz2ze2fbqxna6qqacoex41z /]# systemctl restart nginx
[root@iz2ze2fbqxna6qqacoex41z /]# netstat -anput | grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 2303/nginx: master
成功之后,确认好你的项目是通过nohup java -jar XXX.jar >temp.txt &
不间断运行成功的,最后的&表示后台运行,并将console日志打印到自定义的temp.txt文件中去。