ubuntu22.04离线安装nginx
nginx: download下载地址
cd /data
tar -zxvf nginx-1.27.3.tar.gz
cd nginx-1.27.3
useradd nginx
安装依赖
百度网盘地址如下
通过网盘分享的文件:yilai.zip
链接: https://pan.baidu.com/s/1HNOV-fpBTdskDBl0pR8jug?pwd=1234 提取码: 1234
#安装所有依赖
sudo dpkg -i ./*.deb
cd /data/nginx-1.27.3
下面代码直接执行
./configure \
--prefix=/usr/local/nginx \
--sbin-path=/usr/sbin/nginx \
--conf-path=/etc/nginx/nginx.conf \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--pid-path=/var/run/nginx.pid \
--lock-path=/var/run/nginx.lock \
--http-client-body-temp-path=/do/nginx/tmp/client \
--http-proxy-temp-path=/do/nginx/tmp/nginx/proxy \
--http-fastcgi-temp-path=/do/nginx/tmp/nginx/fcgi \
--http-uwsgi-temp-path=/do/nginx/tmp/nginx/uwsgi \
--http-scgi-temp-path=/do/nginx/tmp/nginx/scgi \
--user=nginx \
--group=nginx \
--with-http_v2_module \
--with-http_ssl_module \
--with-http_realip_module \
--with-http_addition_module \
--with-http_sub_module \
--with-http_dav_module \
--with-http_flv_module \
--with-http_mp4_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_random_index_module \
--with-http_secure_link_module \
--with-http_stub_status_module \
--with-http_auth_request_module \
--with-mail \
--with-mail_ssl_module \
--with-file-aio \
--with-ipv6 \
--with-threads \
--with-stream \
--with-stream_ssl_module
sudo mkdir -p /do/nginx/tmp/client
sudo mkdir -p /do/nginx/tmp/nginx/proxy
sudo mkdir -p /do/nginx/tmp/nginx/fcgi
sudo mkdir -p /do/nginx/tmp/nginx/uwsgi
sudo mkdir -p /do/nginx/tmp/nginx/scgi
#编译
make && make install
#查看版本
nginx -v
nginx 服务配置文件
sudo vi /etc/systemd/system/nginx.service
#内容如下
[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
PIDFile=/run/nginx.pid
ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf
ExecReload=/usr/sbin/nginx -s reload
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
配置/etc/nginx/nginx.conf文件
该配置文件是一个直接监听到端口的配置文件,如果只要监听一个端口直接更改server就行,如果需要监听好几个,可以将其替换成另一个配置文件,再将配置文件统一放入一个目录
如下将/etc/nginx/nginx.conf文件整体替换成以下内容,
include /etc/nginx/conf.d/*.conf;代表监听这个里面的所有配置文件
/etc/nginx/conf.d/这个目录需要自己创建
user root;
worker_processes auto;
error_log /var/log/nginx/error.log notice;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/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 /var/log/nginx/access.log main;
sendfile on;
keepalive_timeout 65;
include /etc/nginx/conf.d/*.conf;
}
在放一个8001.conf文件在/etc/nginx/conf.d/这个目录里
server {
listen 8001;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
#前端文件
location / {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
root /data/web/;
try_files $uri $uri/ /index.html last;
index index.html;
}
#后端
location /admin-api {
proxy_pass http://192.168.1.198:48080/admin-api;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_intercept_errors on;
error_page 301 302 307 = @handle_redirects;
}
location @handle_redirects {
set $saved_redirect_location '$upstream_http_location';
proxy_pass $saved_redirect_location;
}
location /static {
add_header Access-Control-Allow-Origin *;
add_header Access-Control-Allow-Methods 'GET,POST';
add_header Access-Control-Allow-Headers 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization';
alias /mount/static;
try_files $uri $uri/ /index.html last;
proxy_store_access user:rw group:rw all:rw;
index index.html;
expires 7d;
}
#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;
}
}
重启
sudo systemctl restart nginx