本文最后更新于96 天前,其中的信息可能已经过时,如有错误请发送邮件到z18096561915@outlook.com
1.在Linux上安装Nginx
一:安装依赖
yum -y install gcc gcc-c++ make libtool zlib zlib-devel openssl openssl-devel pcre pcre-devel
(如果)yum install 失败
错误:为 repo ‘appstream’ 下载元数据失败 : Cannot prepare internal mirrorlist: No URLs in mirrorlist
这是因为在CentOS8.0中默认不再支持ntp软件包,时间同步将由chrony来实现,像我这种习惯了ntp同步时间的,一时难以去适应chrony。本文将通过wlnmp提供的源,来安装ntp服务
解决方法
如果你仍然需要运行CentOS 8,你可以在/etc/yum.repos.d中更新一下源。使用vault.centos.org代替mirror.centos.org。
进入目录 /etc/yum.repos.d/ 执行一下面代码进行修改
cd /etc/yum.repos.d/
sudo sed -i -e "s|mirrorlist=|#mirrorlist=|g"/etc/yum.repos.d/CentOS-*
sudo sed -i -e "s|#baseurl=http://mirror.centos.org|baseurl=http://vault.centos.org|g"/etc/yum.repos.d/CentOS
二:下载Nginx版本
1.官网直接下载.tar.gz安装包,地址:https://nginx.org/download/
2.使用wget命令下载
wget -c https://nginx.org/download/nginx-1.18.0.tar.gz
如果安装最新版本,可以在官网https://nginx.org/ 查找,上面那个wget下载是我2022年写的1.18.0版本,现在是2024-06-06,nginx最新稳定版本是1.26.1,如想下载最新稳定版本则 wget -c https://nginx.org/download/nginx-1.26.1.tar.gz
改一下url的版本地址就可以
三:解压
tar -zxvf nginx-1.18.0.tar.gz
cd nginx-1.18.0
四:配置
1.不需要SSL(prefix=/usr/local/nginx 为nginx安装的位置)
./configure --prefix=/usr/local/nginx
2.需要SSL
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
五:编译安装
make
make install
六:启动Nginx
# 启动
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
# 重启
/usr/local/nginx/sbin/nginx -s reload
2.配置Nginx
1.找到Nginx安装目录
#我的安装目录在/www/server
cd /www/server/nginx/
#进入nginx的配置文件
vim conf/nginx.conf
这里我配置了两个网站:
- 网站一监听了8081端口 并且网站一的项目文件放在了/www/server/phpmyadmin目录下
- 网站二监听了80端口,项目文件放在了Nginx静态目录html目录中
user www www;
worker_processes auto;
error_log /www/wwwlogs/nginx_error.log crit;
pid /www/server/nginx/logs/nginx.pid;
worker_rlimit_nofile 51200;
stream {
log_format tcp_format '$time_local|$remote_addr|$protocol|$status|$bytes_sent|$bytes_received|$session_time|$upstream_addr|$upstream_bytes_sent|$upstream_bytes_received|$upstream_connect_time';
access_log /www/wwwlogs/tcp-access.log tcp_format;
error_log /www/wwwlogs/tcp-error.log;
include /www/server/panel/vhost/nginx/tcp/*.conf;
}
events
{
use epoll;
worker_connections 51200;
multi_accept on;
}
http
{
include mime.types;
#include luawaf.conf;
include proxy.conf;
lua_package_path "/www/server/nginx/lib/lua/?.lua;;";
default_type application/octet-stream;
server_names_hash_bucket_size 512;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
client_max_body_size 50m;
sendfile on;
tcp_nopush on;
keepalive_timeout 65;
tcp_nodelay on;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 64k;
fastcgi_buffers 4 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 256k;
fastcgi_intercept_errors on;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.1;
gzip_comp_level 2;
gzip_types text/plain application/javascript application/x-javascript text/javascript text/css application/xml application/json image/jpeg image/gif image/png font/ttf font/otf image/svg+xml application/xml+rss text/x-js;
gzip_vary on;
gzip_proxied expired no-cache no-store private auth;
gzip_disable "MSIE [1-6]\.";
limit_conn_zone $binary_remote_addr zone=perip:10m;
limit_conn_zone $server_name zone=perserver:10m;
server_tokens off;
access_log off;
map $http_upgrade $connection_upgrade{
default upgrade;
'' close;
}
upstream webservers{
server 127.0.0.1:8082 weight=90 ;
#server 127.0.0.1:8088 weight=10 ;
}
# 网站一
server
{
listen 8081;
server_name phpmyadmin;
index index.html index.htm index.php;
root /www/server/phpmyadmin;
location ~ /tmp/ {
return 403;
}
include enable-php.conf;
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
}
location ~ .*\.(js|css)?$
{
expires 12h;
}
location ~ /\.
{
deny all;
}
access_log /www/wwwlogs/access.log;
}
#网站二
server {
listen 80;
server_name localhost;
location / {
root html/sky;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# 反向代理,处理管理端发送的请求
location /api/ {
# proxy_pass http://localhost:80/admin/;
proxy_pass http://webservers/admin/;
}
# 反向代理,处理用户端发送的请求
location /user/ {
proxy_pass http://webservers/user/;
}
# WebSocket
location /ws/ {
# proxy_pass http://webservers/ws/;
proxy_pass http://webservers/ws/;
proxy_http_version 1.1;
proxy_read_timeout 3600s;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "$connection_upgrade";
}
}
include /www/server/panel/vhost/nginx/*.conf;
}
2.重启Nginx
我这里使用的宝塔面板直接一键重启或者直接使用命令
#重新加载配置
sudo systemctl reload nginx