在CentOS中安装和使用nginx
概述
本文简单讲述一下,如何快速将一个内网的Web服务通过nginx提供给外网访问,并且启用HTTPS。例如我们部署了一个kubesphere,地址为192.168.202.151:30880,需要通过nginx来提供给外网访问,我们来看看在CentOS上如何进行快速部署。
安装
首先我们通过yum来安装nginx相关的服务
yum install -y nginx
安装完毕之后,我们启动nginx,并检查nginx状态
# 启动nginx
systemctl start nginx
# 查看nginx服务状态
systemctl status nginx
服务正常运行之后,接下来我们创建一个目录,将我们的证书文件放入其中,个人使用可以从阿里云或者腾讯云等云平台申请免费的证书
# 创建目录存放证书,将证书文件复制到里面,示例证书文件为:mstmdev.com_bundle.crt mstmdev.com.key
mkdir /etc/nginx/ssl_cert/
证书准备完毕之后,我们开始最后一步,编辑我们的nginx配置文件
vim /etc/nginx/nginx.conf
以下为修改后的配置文件,目的是将我们服务器上的http请求全部重定向到https上,并将https上的请求转发到内网服务192.168.202.151:30880之中,让我们可以通过外网域名访问我们的服务。
# For more information on configuration, see:
# * Official English Documentation: http://nginx.org/en/docs/
# * Official Russian Documentation: http://nginx.org/ru/docs/
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;
events {
worker_connections 1024;
}
http {
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;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 4096;
include /etc/nginx/mime.types;
default_type application/octet-stream;
# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf;
upstream kubesphere{
server 192.168.202.151:30880;
}
# server {
# listen 80;
# listen [::]:80;
# server_name _;
# root /usr/share/nginx/html;
# # Load configuration files for the default server block.
# include /etc/nginx/default.d/*.conf;
# error_page 404 /404.html;
# location = /404.html {
# }
# error_page 500 502 503 504 /50x.html;
# location = /50x.html {
# }
# }
server {
listen 80;
server_name mstmdev.com;
access_log /var/log/nginx/mstmdev_com_access.log main;
# 将http请求重定向为https
rewrite ^(.*)$ https://$host$1 permanent;
}
# Settings for a TLS enabled server.
#
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name gors.cc;
# root /usr/share/nginx/html;
#
# 这里就是配置我们刚才存放的证书文件的路径
ssl_certificate "/etc/nginx/ssl_cert/mstmdev.com_bundle.crt";
ssl_certificate_key "/etc/nginx/ssl_cert/mstmdev.com.key";
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 10m;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
#
# # Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
#
# error_page 404 /404.html;
# location = /40x.html {
# }
#
# error_page 500 502 503 504 /50x.html;
# location = /50x.html {
# }
access_log /var/log/nginx/https_mstmdev_com.log main;
# 将https请求转发到我们的内网服务192.168.202.151:30880中
location / {
proxy_pass http://kubesphere/;
}
}
}
配置文件修改完毕之后,重启nginx,使配置生效
systemctl restart nginx
此时我们将自己的域名的DNS解析到我们服务器的公网IP地址就可以在外网通过HTTPS地址访问kubesphere后台了。
如果想要在服务器重启后自动启动nginx,可以将其设置为开机启动
systemctl enable nginx
如果不再需要开机启动,将其禁用即可
systemctl disable nginx
更多详细的配置可以参考官方的文档:https://nginx.org/en/docs/
在CentOS中安装和使用nginx的更多相关文章
- 在centos中安装最新版nginx,同时更改官方文档路径错误
nginx的可以使用各平台的默认包来安装,本文是介绍使用源码编译安装,包括具体的编译参数信息. 正式开始前,编译环境gcc g ++开发库之类的需要提前装好,这里默认你已经装好. ububtu平台编译 ...
- 在Centos中安装HustOJ实验记录
hustoj是目前使用最广泛的刷题平台,自建平台是编程教学中重要一环,教,学,测,反思形成闭合回路.目前利用现有的服务器资源,建成了开高开源code中心,一个刷题系统,六月再建一个私有云,私有云不仅是 ...
- 在centos中安装jenkins master测试环境
在centos中安装jenkins 1)安装目录 pwd (/home/AAA) 2)检查java是否安装 [AAA@Centos_AAA jenkins]$ java -version j ...
- centos中安装chromium和flash
安装环境:centos 6.5 64位 在centos中安装chromium 安装Google源 cd /etc/yum.repos.d/ sudo wget http://people.CentOS ...
- Centos中安装Sublime编辑器
Centos中安装Sublime编辑器 1.从官网下载相应操作系统的下的安装包(http://www.sublimetext.com/2),这里下的是linux下的安装包 2.解压安装包,并将其放在/ ...
- centos中安装tomcat6
在centos中安装tomcat6 1)通过yum自动安装tomcat和dependences root@Centos_AAA ~]# yum install tomcat6 [root@Cent ...
- centos中安装tomcat
1.先保证centos中安装了jre的环境. 2.上传tomcat的压缩包到root根目录. 3.切换到根目录 输入命令cd ~ , 然后 ll , 查看上传情况: 4.选中复制压缩文件,输入解压命令 ...
- Docker学习笔记之-在CentOS中安装Docker
上一节演示了如何 通过Xshell连接CentOS服务,链接:Docker学习笔记之-通过Xshell连接 CentOS服务 本节将演示 如何在CentOS中安装 Docker 第一步:更新系统包到最 ...
- centos中安装配置nginx完成之后主机无法访问
原因 VMware中安装centos7,安装配置完成nginx后,主机无法访问.虚拟机没有放行Nginx默认端口80. 解决办法 首先:开启 web 端口 firewall-cmd --permane ...
- CentOS 7 安装php5.6,Nginx,Memcached环境及配置
安装php5.6版本以后不再需要安装Zend Guard,而是用yum命令安装php-opcache及php-pecl-apcu就可以有效的提高php执行速度. 1. 配置yum源 事先确认yum源的 ...
随机推荐
- github进不去
发现github进不去了:百度解决方案:修改hosts表,文件位置在C:\Windows\System32\drivers\etc 记事本打开,尝试在最后添加140.82.112.4 github.c ...
- js中的call()、apply()、bind()方法
var a= { name:"李四", age: "五岁", text: function() { return this.name+ " " ...
- 【逆向】x64dbg设置条件断点 比较内存字符串是否相等
前言 在OD中可以设置条件断点,通过表达式对字符串数据进行比较,比如在CreateFile打开某个特定文件的时候让调试器中断.但是在x32dbg.x64dbg中因为表达式只支持整数,不支持字符串和其它 ...
- chatGpt启示
在应用软件领域实现自动化编程,我现在确实不怀疑了,只是还要等几年的问题.所以我的建议是:如果还想撸代码,可以往基础软件方向走,或者直接去大公司做 AI 工具:不想搞软件了,就去多了解业务吧.早做准备早 ...
- 内存模型和名称空间复习题(c++ prime plus )
第一题 注意到setgolf()被重载,可以这样使用其第一个版本: golf ann; setgolf(ann, "Ann Birdfree", 24); 上述函数调用提供了存储在 ...
- java图形化界面编程(AWT)
1.AWT编程简介 在JDK发布时,sun公司提供了一套基本的GUI类库,这个GUI类库希望可以在所有平台下都能运行,这套基本类库被称为"抽象窗口工具集",它为java应用程序提供 ...
- C 语言 scanf 格式化输入函数
C 语言 scanf 格式化输入函数 函数概要 scanf 函数从标准输入流中读取格式化字符串.与 printf 格式化输出函数相反,scanf 函数是格式化输入函数. 函数原型 #include & ...
- PVE设置硬盘休眠并解决经常唤醒问题
查询硬盘编号: ls -l /dev/disk/by-id/ 查询硬盘状态: smartctl -i -n standby /dev/sda |grep "mode"|awk '{ ...
- Verilog 预编译
Verilog 预编译 Verilog 语言支持宏定义(`define),参数 parameter,局域参数(localparam)以及`include等内容.这些数据常量的支持极大方便数字系统设计. ...
- vue移动端购物商场首页制作
1.搭建项目框架 新建首页主组件及其子组件并将子组件展示出来 2.封装所需接口 3.编写轮播图组件 <template> <div id="swipercom"& ...