一、安装过程
# 下载 Nginx 安装包
wget http://nginx.org/download/nginx-1.26.3.tar.gz
# 解压安装包
tar -zxvf nginx-1.26.3.tar.gz
# 下载并解压 PCRE 库
wget https://sourceforge.net/projects/pcre/files/pcre/8.45/pcre-8.45.tar.gz
tar -zxvf pcre-8.45.tar.gz
# 进入 Nginx 源代码目录
cd nginx-1.26.3
# 配置、编译和安装 Nginx,指定 PCRE 库路径
./configure --with-pcre=../pcre-8.45
make
sudo make install
# 启动 Nginx
sudo /usr/local/nginx/sbin/nginx
# 打开 Nginx 配置文件进行编辑
sudo nano /usr/local/nginx/conf/nginx.conf
# 修改 listen 指令
# 将 listen 80; 修改为 listen 8080;
# 保存并退出
# 重新加载 Nginx 配置
sudo /usr/local/nginx/sbin/nginx -s reload
二、常用命令
# 启动 Nginx
sudo /usr/local/nginx/sbin/nginx
# 停止 Nginx
sudo /usr/local/nginx/sbin/nginx -s stop
# 重新加载配置
sudo /usr/local/nginx/sbin/nginx -s reload
# 确认 Nginx 进程的用户
grep 'user' /usr/local/nginx/conf/nginx.conf
#查看日志
sudo cat /usr/local/nginx/logs/error.log
三、配置文件
#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;
upstream zaingray{
server 127.0.0.1:8080 weight=4;
server 127.0.0.1:8081 weight=1;
}
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root C:/Users/10938/Desktop/my-vue3-vite-project/dist;
index index.html index.htm;
# 新增下面这句,其他是默认nginx配置
# 解决部分前端框架的路由问题,在浏览器刷新报错404
try_files $uri $uri/ /index.html;
}
location /app-dev/ {
proxy_pass http://zaingray/;
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_set_header X-Forwarded-Proto $scheme;
}
#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 cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}