美文网首页
单机,前后端分离,Nginx反向代理

单机,前后端分离,Nginx反向代理

作者: cinjep | 来源:发表于2021-01-01 17:57 被阅读0次
# 本配置仅用于测试开发环境

环境:
Debian10 + Nginx1.14 + PHP7.3

nginx.conf 配置:

user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;
events {
    worker_connections 768;
        multi_accept on;
}
http {
    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;
    include /etc/nginx/mime.types;
    default_type application/octet-stream;
    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;
    gzip on;
    include sites/*.conf;        #前后端站点配置文件在sites文件夹中
}

html 前端站点配置文件 html.conf

server {
    listen 80 default_server;
    listen [::]:80 default_server;
    root /var/www/html;          #前端文件路径
    index index.html;
    server_name html_server;
    location / {
        try_files $uri $uri/ =404;
    }
    location ^~/api {
        proxy_pass http://127.0.0.1:8080/;
    }
}

API后端接口站点配置文件 api.conf

server {
    listen 8080;
    #listen [::]:8080 api_server;
    root /var/www/api;           #后端API文件路径
    index index.html index.php;
    server_name api_server;
    
    add_header Access-Control-Allow-Origin *;
    add_header Access-Control-Allow-Credentials true;
    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';  

    location ~ \.php$ {
        fastcgi_split_path_info ^(.+?\.php)(/.*)$;
        try_files $fastcgi_script_name =404;
        set $path_info $fastcgi_path_info;
        fastcgi_param PATH_INFO $path_info;
        fastcgi_index index.php;
        include fastcgi.conf;
        fastcgi_pass unix:/run/php/php7.3-fpm.sock;
    }
}

相关文章

网友评论

      本文标题:单机,前后端分离,Nginx反向代理

      本文链接:https://www.haomeiwen.com/subject/wlmroktx.html