阿里云CentOS8自定义安装nginx
1.安装工具和库
任意目录下执行下面命令
|
1
|
yum -y install gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel |
# PCRE是一个Perl库,包括 perl 兼容的正则表达式库。nginx 的 http 模块使用 pcre 来解析正则表达式
# zlib库提供了很多种压缩和解压缩的方式, nginx 使用 zlib 对 http 包的内容进行 gzip
2.下载并解压nginx
在/usr/local下建立一个nginx目录
cd /usr/local
mkdir nginx
进入nginx目录
cd nginx
再执行下载和解压命令
|
1
2
|
wget -c https://nginx.org/download/nginx-1.18.0.tar.gztar -zxvf nginx-1.18.0.tar.gz |
3.编译与安装nginx
cd /usr/local/nginx/nginx-1.18.0
执行如下命令
1,configure
|
1
|
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_v2_module --with-http_sub_module --with-http_gzip_static_module --with-pcre --with-stream |
#--prefix 指定安装路径
#--with-http_stub_status_module 允许查看nginx状态的模块
# --with-http_ssl_module 支持https的模块
# --with-stream 支持stream的模块
2,编译并安装
make && make install
4.启动nginx
进入到安装nginx目录下面的sbin
cd /usr/local/nginx/sbin
启动命令
./nginx
打开浏览器访问你的IP地址,显示此页面说明nginx启动成功。
其他命令:
./nginx -s quit: (温和)此方式停止步骤是待nginx进程处理任务完毕进行停止。 ./nginx -s stop: (强硬)此方式相当于先查出nginx进程id再使用kill命令强制杀掉进程。
./nginx -s reload 重启nginx(不推荐此方法,推荐先停止在启动)
附上nginx.conf配置如下(包含一级和二级域名):
#user nobody;
user root;
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;
client_max_body_size 100M;
#gzip on;
server {
listen 80;
server_name 127.0.0.1;
rewrite ^(.*)$ https://$host$1 permanent;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root /usr/local/nginx/dist;
index index.html index.htm;
try_files $uri $uri/ /index.html;
}
location /api {
proxy_pass http://127.0.0.1:8080/api;
proxy_cookie_path / /api;
# proxy_redirect default;
# rewrite ^/api/(.*) /$1 break;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
}
location /label {
alias /usr/local/nginx/label;
index index.html index.htm;
try_files $uri $uri/ /index.html;
}
location /markapi {
proxy_pass http://127.0.0.1:9090/markapi;
proxy_cookie_path / /markapi;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
}
#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;
}
}
# HTTPS server
#
server {
listen 443 ssl;
#server_name localhost;
server_name www.aabbcc.cn;
# root /usr/local/nginx/dist;
# index index.htm;
# ssl on;
ssl_certificate /usr/local/nginx/ssl_cert/aabbcc.cn.crt;
ssl_certificate_key /usr/local/nginx/ssl_cert/aabbcc.cn.key;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers ECDH:AESGCM:HIGH:!RC4:!DH:!MD5:!3DES:!aNULL:!eNULL;
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
# dotCode service
location / {
root /usr/local/nginx/dist;
try_files $uri $uri/ @router;
index index.html index.htm;
}
location /api {
proxy_pass http://127.0.0.1:8080/api;
proxy_cookie_path / /api;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
}
# label service
# location /label {
# root /usr/local/nginx/label;
# index index.html;
# try_files $uri $uri/ /index.html;
# }
location /label {
alias /usr/local/nginx/label/;
try_files $uri $uri/ /label/index.html;
index index.html index.htm;
}
location @router {
rewrite ^.*$ /index.html last;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
location /markapi {
proxy_pass http://127.0.0.1:9090/markapi;
proxy_cookie_path / /markapi;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
}
}
}

浙公网安备 33010602011771号