前言
linux服務(wù)器nginx配置ssl證書。
一、ssl證書
需要申請域名,然后域名解析到你的外網(wǎng)服務(wù)器ip,然后申請ssl證書,然后下載下來,一般ssl證書可以通過 tomcat nginx等配置;
二、nginx配置ssl證書
- 更新yum
yum update yum
- gcc安裝
yum -y install gcc gcc-c++ autoconf automake make
- 其他安裝
yum -y install zlib zlib-devel pcre-devel openssl openssl-devel
- 找個位置下載nginx 例如: /usr/local/src
wget https://nginx.org/download/nginx-1.24.0.tar.gz
- 當(dāng)前位置解壓
tar -zxvf nginx-1.24.0.tar.gz
- 創(chuàng)建用戶組 用戶
groupadd nginx
useradd nginx -g nginx -s /sbin/nologin -M
- 進(jìn)入解壓后的目錄 編譯nginx并加入ssl 安裝
# 配置ssl
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --user=nginx --group=nginx
# 安裝編譯
make &&make install
- 修改nginx文件夾歸屬
chown -R nginx:nginx /usr/local/nginx
- 可在安裝路徑中 /usr/local/nginx/sbin 啟動nginx
#啟動
./nginx
#重啟
./nginx -s reload
#關(guān)閉
./nginx -s stop
#驗證配置nginx.conf正確
./nginx -t
- 訪問地址 ip 是否能看到nginx默認(rèn)頁面.ok后,停止nginx
- 下載證書 改名字,上傳到nginx的安裝目錄的conf的cert文件下 默認(rèn)安裝位置為: /usr/local/nginx/conf/cert
server.crt
server.key
- 修改nginx.conf 所在位置為 /usr/local/nginx/conf
● 代理vue前端頁面
● 代理后端接口為/api/ 去掉此前綴 轉(zhuǎn)發(fā)到服務(wù)器的9000端口
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include 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 logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name localhost;
location / {
# 前端地址在 /usr/local/nginx/html/dist
root html/dist/;
index index.html index.htm;
# 刷新404
try_files $uri $uri/ /index.html;
}
# 代理服務(wù)端接口
location /api/ {
default_type application/json;
proxy_pass http://ip:9000;
rewrite /api/(.*) /$1 break;
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_pass_request_headers on;
proxy_next_upstream error timeout;
}
#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 html;
}
}
}
- 配置證書的完整 nginx.conf
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include 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 logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name localhost;
location / {
root html/dist/;
index index.html index.htm;
try_files $uri $uri/ /index.html;
}
# 代理服務(wù)端接口
location /dev-api/ {
default_type application/json;
proxy_pass http://ip:9000;
rewrite /dev-api/(.*) /$1 break;
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_pass_request_headers on;
proxy_next_upstream error timeout;
}
# 代理圖片服務(wù)接口
location /image/ {
default_type application/json;
rewrite /image/(.*) /$1 break;
proxy_pass https://test-lsdj.obs.cn-north-4.myhuaweicloud.com/;
proxy_pass_request_headers on;
proxy_next_upstream error timeout;
}
#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 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;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server ssl 配置
#
server {
listen 443 ssl;
# 自己申請的域名
server_name www.baidu.com;
ssl_certificate cert/server.crt;
ssl_certificate_key cert/server.key;
# 一下配置同之前的配置即可
location / {
root html/dist/;
index index.html index.htm;
try_files $uri $uri/ /index.html;
}
# 代理服務(wù)端接口
location /api/ {
default_type application/json;
proxy_pass http://ip:9000;
rewrite /api/(.*) /$1 break;
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_pass_request_headers on;
proxy_next_upstream error timeout;
}
}
13 . 可在安裝路徑中 /usr/local/nginx/sbin 啟動,驗證是否正確
./nginx
也就是相當(dāng)于,沒有ssl的時候, 配置好代理后可以用驗證完畢;
當(dāng)配置ssl的時候,之前的配置就用不到了,因為ssl是443端口,所以需要全部重新配置;并配置ssl開啟 以及證書相關(guān);文章來源:http://www.zghlxwxcb.cn/news/detail-847057.html
總結(jié)
至此 已經(jīng)可以成功通過域名訪問到服務(wù)器的頁面了~~文章來源地址http://www.zghlxwxcb.cn/news/detail-847057.html
到了這里,關(guān)于nginx ssl證書配置的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!