一、安装Nginx
- 安装Nginx
mkdir application
cd application/
wget https://nginx.org/en/download/nginx-1.22.0.tar.gz
tar -zxvf nginx-1.22.0.tar.gz
- 运行初始化配置
cd ./nginx-1.22.0
./configure
- 如果出现提示错误, 可能是依赖包乜有安装需要安装
# 提示 pcre library
yum install -y pcre pcre-devel
#提示 c++ compile flags
yum -y install gcc gcc-c++
#提示 zlib library
yum install -y zlib-devel
安装后再次运行
cd ./nginx-1.22.0
./configure
- 编译
make
- 安装
make install
- 运行 nginx
whereis nginx
nginx: /usr/local/nginx
cd /usr/local/nginx
./sbin/nginx
- 查看是否运行成功
ps -ef | grep nginx
- 浏览器访问 localhost:80
- 其他命令
cd /usr/local/nginx
./sbin/nginx #启动
./sbin/nginx -s reload #'重启
./sbin/nginx -s stop #关闭
./sbin/nginx -s quit #请求处理完后才关闭
参考:https://blog.csdn.net/Cs_Mervyn/article/details/120075477
二、 Nginx 的 SSL 模块安装
- 查看 nginx 是否安装 http_ssl_module 模块。
$ /usr/local/nginx/sbin/nginx -V
如果出现 configure arguments: –with-http_ssl_module, 则已安装(下面的步骤可以跳过,进入 nginx.conf 配置)。
1.1 配置ssl
$ cd nginx-1.20.0
$ ./configure --prefix=/usr/local/nginx --with-http_ssl_module
1.2 重新编译
make
命令编译(使用make install会重新安装nginx),此时当前目录会出现 objs 文件夹。
1.3
用新的 nginx 文件覆盖当前的 nginx 文件。
$ cp ./objs/nginx /usr/local/nginx/sbin/
1.4 验证
再次查看安装的模块(configure arguments: –with-http_ssl_module说明ssl模块已安装)。
$ /usr/local/nginx/sbin/nginx -V
nginx version: nginx/1.15.9
configure arguments: --with-http_ssl_module
三、配置证书
- 进入配置文件中
cd /usr/local/nginx/conf/
cat 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是 http请求的 直接都注释了
#server {
# listen 80;
# server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
# location / {
# root html;
# index index.html index.htm;
#}
#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;
#}
#}
下面的HTTPS server 进行配置
# 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
#
server {
listen 443 ssl;
server_name XXX.XXX.com;
# 设置重定向
location / {
proxy_pass http://127.0.0.1:8080; # 转发规则
proxy_set_header Host $proxy_host; # 修改转发请求头,让8080端口的应用可以受到真实的请求
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
# 设置证书的信息
ssl_certificate /usr/local/nginx/XXX.pem;
ssl_certificate_key /usr/local/nginx/XXX.key;
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
}
}
最后重启 输入配置的server_name就可以进行访问;
cd /usr/local/nginx
./sbin/nginx -s reload #'重启
留在最后的话
Nginx的作用是什么
Nginx的正向代理 和反向代理是什么意思