常见nginx配置

常见基本配置

可以参考:
https://zhuanlan.zhihu.com/p/92430690

worker_processes  1;


#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;

    keepalive_timeout  65;



    server {
        underscores_in_headers on;
        listen       11399;
        server_name  localhost;
        client_max_body_size 300m;
        location / {
          add_header 'Access-Control-Allow-Origin' '*'; #这里必须指定为$http_orgin,明确指定可以跨域请求的域名,替代*
    	  add_header 'Access-Control-Allow-Headers' '*';
    	  add_header 'Access-Control-Allow-Methods' "GET,POST,PUT,DELETE,OPTIONS,FM";
          root   E:\\platform\\dist;
          index  index.html index.htm;
		  try_files $uri $uri/ /index.html;
        }
        

        
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
		
        location /api/ {
		    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-NginX-Proxy true;
            proxy_pass  http://localhost:8010;
        }
		
		location /auth/ {
		    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-NginX-Proxy true;
            proxy_pass  http://localhost:8010;
        }
		
		location /mapdata {  
            include nginx_cors; 
            root E:\\platform;  
                index  index.html index.htm;
                expires 30d;  
        }
		# 将G:和/3ddata拼接,而不是将/3ddata替换成G:
		location /3ddata {
		    include nginx_cors; 
            root G:;  
                index  index.html index.htm;
                expires 30d;
		}
		
		location /arcgis { 
            include nginx_cors; 
            proxy_set_header Host $host:$server_port;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Scheme $scheme;
            proxy_pass http://172.16.218.46:6080;
        }

        location ^~/base/ {
			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-NginX-Proxy true;
			rewrite ^/base/(.*)$ /$1 break;
			proxy_pass http://localhost:8010;
        }
		
		location ^~/loc/ {
			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-NginX-Proxy true;
			rewrite ^/loc/(.*)$ /$1 break;
			proxy_pass http://172.16.218.223:6080;
       }
    }
	server {
        underscores_in_headers on;
        listen       8083;
        server_name  localhost;
        client_max_body_size 300m;
		location / {
          add_header 'Access-Control-Allow-Origin' '*'; #这里必须指定为$http_orgin,明确指定可以跨域请求的域名,替代*
    	  add_header 'Access-Control-Allow-Headers' '*';
    	  add_header 'Access-Control-Allow-Methods' "GET,POST,PUT,DELETE,OPTIONS,FM";
          root   E:\\platform\\dist2\\;
          index  index.html index.htm;
		  try_files $uri $uri/ /index.html;
        }
	}

}

具体详解

nginx监听端口

在server下加入

 underscores_in_headers on;
        listen       11399;
        server_name  localhost;
        client_max_body_size 300m;

解决跨域问题

在server下加入

        location / {
          add_header 'Access-Control-Allow-Origin' '*'; #这里必须指定为$http_orgin,明确指定可以跨域请求的域名,替代*
    	  add_header 'Access-Control-Allow-Headers' '*';
    	  add_header 'Access-Control-Allow-Methods' "GET,POST,PUT,DELETE,OPTIONS,FM";
          root   E:\\platform\\dist;
          index  index.html index.htm;
		  try_files $uri $uri/ /index.html;
        }

具体如何实现映射

在server下加入

		# 将G:和/3ddata拼接,而不是将/3ddata替换成G:
		location /3ddata {
		    include nginx_cors; 
            root G:;  
            index  index.html index.htm;
            expires 30d;
		}

一要注意是拼接而不是替换

在某个文件夹下实现文件目录

在server下的location /3ddata中加入
一些参数说明:
autoindex on; # 开启目录文件列表
autoindex_exact_size on; # 显示出文件的确切大小,单位是bytes
autoindex_localtime on; # 显示的文件时间为文件的服务器时间
charset utf-8; # 避免中文乱码

另外,如果希望请求文件是下载而不是显示内容,可以通过添加下面参数实现:
add_header Content-Disposition attachment;
如:

		# 将G:和/3ddata拼接,而不是将/3ddata替换成G:
		location /3ddata {
		    include nginx_cors; 
            root G:; 
            autoindex on; # 开启目录文件列表
            autoindex_exact_size on; # 显示出文件的确切大小,单位是bytes
            autoindex_localtime on; # 显示的文件时间为文件的服务器时间
            charset utf-8; # 避免中文乱码 
            index  index.html index.htm;
            expires 30d;
		}

常见命令

nginx.exe -s stop
nginx.exe -s reload
nginx.exe

遇到目录无法访问问题

可参考
中文问题:
https://blog.csdn.net/Mercury_cc/article/details/130120764
缓冲不足问题:
https://www.cnblogs.com/opensmarty/p/17222060.html

posted @ 2023-09-15 10:29  Coder-Wang  阅读(23)  评论(0编辑  收藏  举报