Nginx 在 linux 上的安装与配置
解决https默认的443端口代理到服务器本地的3000端口
前言
在做自己的个人博客的过程中,后端使用express启动的是3000端口,本来想尝试着启动80端口的,但是启动80端口显示该端口已被占用(也没有去查看那个进程占用了该端口)。每次访问博客都需要在域名后面加上端口号才可以访问,所以就想着使用Nginx做反向代理。第一次使用Nginx,所以就记录下来
Linux: Centos8
下载Nginx及相关工具
Nginx 官方网址 nginx.org/en/download…
- 进入到
/usr/local/src
目录下
cd /usr/local/src
- 下载Nginx
wget http://nginx.org/download/nginx-1.14.2.tar.gz
- 下载其他相关工具
yum install gcc openssl openssl-devel pcre pcre-devel zlib zlib-devel -y
安装Nginx
- 解压Nginx的安装包
tar -zxvf nginx-1.14.2.tar.gz
- 进入到解压后的目录内,开始执行配置命令
cd nginx-1.14.2
配置命令,其中需要用到https协议,所以需要--with-http_ssl_module
模块,--prefix
后面是安装路径,可以自定义。
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
- 配置完成后进行安装,使用
make && make install
make
make install
安装完成后,会在/usr/local/nginx
目录中生成安装文件,其中有一个conf文件夹,需要对里面的nginx.conf
文件做反向代理修改。
- 进入到配置文件目录
cd /usr/local/nginx/conf
- 修改nginx.conf文件
vi nginx.conf
(vi + 文件名: 命令可以对文件进行修改,如果没有权限,可以执行su
命令切换成root权限)
打开nginx.conf文件,修改成如下配置,主要修改该server
下的信息
- 注释掉80端口的信息
ssl_certificate
,ssl_certificate_key
后面必须带上ssl证书。(可以使用阿里云免费提供的ssl证书)proxy_pass https://127.0.0.1:3000;
会将请求代理至3000端口。
#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;
#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;
#}
}
# 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 localhost;
ssl_certificate /usr/local/nginx/ssl/cert.pem;
ssl_certificate_key /usr/local/nginx/ssl/cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
location / {
proxy_pass https://127.0.0.1:3000;
root html;
index index.html index.htm;
}
}
}
以上便完成了Nginx的配置。
启动Nginx
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
完成
当访问域名时,默认的443端口就会代理到服务器的3000端口。