linux中配置nginx静态资源路径下载文件,前端资源路径,后端代理及nginx的相关操作

  • nginx静态资源路径下载文件,前端资源路径,后端代理
  • linux中nginx的相关操作

nginx静态资源路径下载文件,前端资源路径,后端代理

下面是我的nginx配置,看注释,根据自己的项目情况来进行配置

worker_processes  1;  # 工作进程数,通常设置为 CPU 核心数

events {
    worker_connections  1024;  # 每个工作进程允许的最大连接数
}

http {
    include       mime.types;  # 包含 MIME 类型配置文件
    default_type  application/octet-stream;  # 默认的 MIME 类型
    sendfile        on;  # 开启 sendfile 功能,提高文件传输效率
    keepalive_timeout  65;  # 客户端与服务器之间的空闲超时时间

 # 第一个 server 块的配置
server {
        listen       8101;  # 监听端口号
        server_name  localhost;  # 服务器名称
        charset utf-8;  # 字符编码

        #前端静态资源文件配置
        location / {
            root   /mnt/zhifei/dist;  # 前端解压后的文件存放的根目录
            try_files $uri $uri/ /index.html;   #在请求的文件或目录不存在时,重定向到指定的文件
            index  index.html index.htm;  # 默认的索引文件
        }
        #后端配置 /api/ 开头的URL反向代理到http://localhost:9898/ 
       location /api/ {
            proxy_set_header Host $http_host;  # 设置代理请求的 Host 头
            proxy_set_header X-Real-IP $remote_addr;  # 设置代理请求的真实 IP 头
            proxy_set_header REMOTE-HOST $remote_addr;  # 设置代理请求的远程主机头
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;  # 设置代理请求的转发头
            proxy_pass http://localhost:9898/;  # 代理到的后端服务器地址
        }

        #可下载的静态资源路径配置
        #文件需要放在/mnt/zhifei/static/下面 访问地址为http://你的ip:监听的端口/static/文件名称
        location /static/ {
            alias /mnt/zhifei/static/;  # 静态文件目录的别名
        }

server {
        listen       8882;
        server_name  localhost;


        location / {
            root /zhifei/platform/dist;
            try_files $uri $uri/ /index.html;
            index  index.html index.htm;
        }

}

server {
        listen       9088;
        server_name  localhost;
        charset utf-8;

        location / {
            root   /zhifei/platform/html/dist;
            try_files $uri $uri/ /index.html;
            index  index.html index.htm;
        }
        
        location /prod-api/ {
            proxy_set_header Host $http_host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header REMOTE-HOST $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_pass http://localhost:8085/;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
}   

server {
        listen       80;
        server_name  localhost;

        location / {
            proxy_pass http://127.0.0.1:9990;
            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 REMOTE-HOST $remote_addr;
        }
        


}

    

server {
    listen 443 ssl;
   
    server_name dy.sichuan-zhifei.com; 
    root html;
    index index.html index.htm;
    ssl_certificate cret/dy.pem; 
    ssl_certificate_key cret/dy.key; 
    ssl_session_timeout 5m;
    ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
    
    ssl_protocols TLSv1.1 TLSv1.2 TLSv1.3; 
    ssl_prefer_server_ciphers on;

    location / {
        proxy_pass http://127.0.0.1:9990;
    }
}

linux中nginx的相关操作

1.全局配置后启动nginx

sudo systemctl restart nginx

2.全局配置后关闭nginx

sudo systemctl stop nginx

如果不想全局配置,可以使用下面的命令

3.查看nginx运行状态

systemctl status nginx

4.杀死所有nginx进程

sudo killall nginx

5.启动nginx

/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

nginx安装目录 -c nginx配置文件目录

本文来自网络,不代表协通编程立场,如若转载,请注明出处:https://net2asp.com/dfdf3ad18b.html