记录一下nginx的配置

95 阅读1分钟

记录一下nginx的配置

server {
    listen       80;
    server_name  localhost;

    #access_log  /var/log/nginx/host.access.log  main;
    
    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
        try_files $uri $uri/ /index.html;
    }
    #location ~* \.(js|css|png|gif|ico|svg|webp|woff|woff2)$ {
    #    expires max;
    #    log_not_found off;
    #}

    location /web/ {
        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;
        # websocket参数
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_pass  http://127.0.0.1:8888/;    #node api server
    }

    location /h5/ {
        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;
        # websocket参数
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_pass  http://127.0.0.1:8888/;    #node api server
    }

    #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   /usr/share/nginx/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;
    #}
}

本来我以为如果本地有两个项目,如果想要跳转过去应该是这样

nginx的前端目录 /usr/share/nginx/html, 这个目录里面放置了多个项目,比如web、h5等

location /web {
    root   /usr/share/nginx/html/web;
    index  index.html index.htm;
    try_files $uri $uri/ /index.html;
}
location /h5 {
    root   /usr/share/nginx/html/h5;
    index  index.html index.htm;
    try_files $uri $uri/ /index.html;
}

看网上有的说 root 对应只能有一个,把第二个root改成alias,但是都不行

所以在这里记录一下,这个配置已经够用了

location / {
    root   /usr/share/nginx/html; 
    index  index.html index.htm;
    try_files $uri $uri/ /index.html;
}
  • root /usr/share/nginx/html
    • 当访问里面的项目比如:web、h5等,应该就是这样localhost:80/web/index.htmllocalhost:80/h5/index.html
  • try_files $uri $uri/ /index.html
    • 这个配置是解决刷新页面会404,如果不是hash路由,是history的时候,刷新页面需要重新回到首页,或者做特殊处理。

其他的配置基本就是前端的请求代理,其他暂时没有涉及,还有缓存和压缩等...

 proxy: {
  '/upload': 'http://localhost:4567/',
  '/api': {
    target: 'http://123.com',
    changeOrigin: true,
    rewrite: path => path.replace(/^\/api/, '')
  }
}