Detailed nginx configuration, nginx cross-domain issues


foreword

This article introduces the configuration of nginx in the LNMP environment, realizes domain name access and detailed explanations of nginx configuration issues. For the installation of nginx in the Linux environment, you can refer to centos7 to build php7.1.3+mysql5.7+nginx1.11


Change the hosts file

Set the local domain name to be accessed, for example: velocityerp.top

#修改hosts文件
vi /etc/hosts
#如下 增加127.0.0.1 velocityerp.top
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6

127.0.0.1 velocityerp.top

Add velocityerp.top.conf configuration file

Create a new velocityerp.top.conf file under /usr/local/nginx/conf/vhosts (the installation path may be different for each person), the content is as follows (generally there are no special requirements, just configure as follows)

server {
    
    
	listen        80;  #侦听端口
	server_name  velocityerp.top; #网站域名
	root   "/mnt/hgfs/project/www/erp/public"; #代码地址
	location / {
    
    
		index index.php index.html error/index.html; 
		if (!-e $request_filename) {
    
     # 如果请求的路径找不到,则nginx执行下面的复写
			rewrite  ^(.*)$  /index.php?s=/$1  last; #意思是假如访问www.baidu.com/test.html,如果找不到,就跳转到www.baidu.com/index.php/test.html,可用于配置访问路径省略index.php
			break;
		}
	}
	location ~ \.php(.*)$ {
    
     # 匹配所有以.php结尾的请求 区分大小写
		fastcgi_pass   127.0.0.1:9000;  #侦听本地9000端口,php-fpm
		fastcgi_index  index.php; #如果请求url以'/'结尾,则在后面追加index.php,在这里设置其实不生效,以为前面已经限制以.php结尾的请求,习惯用法
		fastcgi_split_path_info  ^((?U).+\.php)(/?.+)$; #非贪婪匹配
		fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name; #在浏览器中访问的.php文件,实际读取的是 $document_root(网站根目录)下的.php文件 -- 也就是说当访问127.0.0.1/index.php的时候,需要读取网站根目录下面的index.php文件
		fastcgi_param  PATH_INFO  $fastcgi_path_info; # 脚本请求的值
		fastcgi_param  PATH_TRANSLATED  $document_root$fastcgi_path_info; #解析
		include        fastcgi_params;
	}
}

restart nginx

service nginx restart

Just visit velocityerp.top locally

Nginx configures header parameters (resolving cross-domain), interface side configuration

server {
    
    
    listen        80;
    server_name  api.velocitypublic.top;
    root   "/mnt/hgfs/project/www/velocitypublic-api/public";
    add_header 'Access-Control-Allow-Origin' '*'; #允许的请求来源,* 为所有
    add_header 'Access-Control-Allow-Credentials' 'true' ; #是否允许浏览器后续请求携带认证信息(cookies),该值只能是true,如果服务器不要浏览器发送Cookie,删除该字段即可。
    add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS'; #的值是逗号分隔的一个字符串,表明服务器支持的所有跨域请求的方法。注意,返回的是所有支持的方法,而不单是浏览器请求的那个方法。这是为了避免多次"预检"请求
    add_header 'Access-Control-Allow-Headers' 'Authorization,Content-Type,Accept,Origin,User-Agent,DNT,Cache-Control,X-Mx-ReqToken,Keep-Alive,X-Requested-With,Pragma,Cache-Control,If-Modified-Since,token'; #浏览器请求包括Access-Control-Request-Headers字段,则Access-Control-Allow-Headers字段是必需的。它也是一个逗号分隔的字符串,表明服务器支持的所有头信息字段,不限于浏览器在"预检"中请求的字段。
    add_header 'Access-Control-Max-Age' 1728000; 该字段可选,用来指定本次预检请求的有效期,单位为秒。上面结果中,有效期是20天(1728000秒),即允许缓存该条回应1728000秒(即20天),在此期间,不用发出另一条预检请求
    location / {
    
    
        index index.php index.html error/index.html;
        if (!-e $request_filename) {
    
    
            rewrite  ^(.*)$  /index.php?s=/$1  last;
            break;
        }
    }
    location ~ \.php(.*)$ {
    
    
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_split_path_info  ^((?U).+\.php)(/?.+)$;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        fastcgi_param  PATH_INFO  $fastcgi_path_info;
        fastcgi_param  PATH_TRANSLATED  $document_root$fastcgi_path_info;
        include        fastcgi_params;
    }
}

nginx reverse proxy (solve cross-domain), sender configuration

server {
    
    
	listen        80;
	server_name  tp5.top;
	root   "/mnt/hgfs/project/www/tp5/public";
	location / {
    
    
		index index.php index.html error/index.html;
		if (!-e $request_filename) {
    
    
			rewrite  ^(.*)$  /index.php?s=/$1  last;
			break;
		}
	}
	location ~ \.php(.*)$ {
    
    
		fastcgi_pass   127.0.0.1:9000;
		fastcgi_index  index.php;
		fastcgi_split_path_info  ^((?U).+\.php)(/?.+)$;
		fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
		fastcgi_param  PATH_INFO  $fastcgi_path_info;
		fastcgi_param  PATH_TRANSLATED  $document_root$fastcgi_path_info;
		include        fastcgi_params;
	}

	location /api {
    
    
            proxy_pass   http://api.velocitypublic.top/index.php/api/auth/sliding_code; # 解决跨域,访问tp5.top/api 会代理到api.velocitypublic.top域名下,绕过浏览器同源策略检测
        }
}

nginx port forwarding (solve cross-domain)

server {
    
    
	listen        80;
	server_name  tp5.top;
	root   "/mnt/hgfs/project/www/tp5/public";
	location / {
    
    
		index index.php index.html error/index.html;
		if (!-e $request_filename) {
    
    
			rewrite  ^(.*)$  /index.php?s=/$1  last;
			break;
		}
	}
	location ~ \.php(.*)$ {
    
    
		fastcgi_pass   127.0.0.1:9000;
		fastcgi_index  index.php;
		fastcgi_split_path_info  ^((?U).+\.php)(/?.+)$;
		fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
		fastcgi_param  PATH_INFO  $fastcgi_path_info;
		fastcgi_param  PATH_TRANSLATED  $document_root$fastcgi_path_info;
		include        fastcgi_params;
	}
    location /api/ {
    
    
            # 把 /api 路径下的请求转发给真正的后端服务器
            proxy_pass http://localhost:189/;

            # 把host头传过去,后端服务程序将收到your.domain.name, 否则收到的是localhost:189
            proxy_set_header Host $http_host;

            # 把cookie中的path部分从/api替换成/service
            proxy_cookie_path /api /;

            # 把cookie的path部分从localhost:189替换成your.domain.name
            proxy_cookie_domain localhost:189 rampow.top;

            # 获取用户真实ID
            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;
        }

Nginx internal jump

server {
    
    
	listen        80;
	server_name  tp5.top;
	root   "/mnt/hgfs/project/www/tp5/public";
	location / {
    
    
		index index.php index.html error/index.html;
		if (!-e $request_filename) {
    
    
			rewrite  ^(.*)$  /index.php?s=/$1  last;
			break;
		}
	}
	location ~ \.php(.*)$ {
    
    
		fastcgi_pass   127.0.0.1:9000;
		fastcgi_index  index.php;
		fastcgi_split_path_info  ^((?U).+\.php)(/?.+)$;
		fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
		fastcgi_param  PATH_INFO  $fastcgi_path_info;
		fastcgi_param  PATH_TRANSLATED  $document_root$fastcgi_path_info;
		include        fastcgi_params;
	}
	location ^~ /api {
    
    
        alias   /mnt/hgfs/project/www/wms_api/public; #目录别名,root与alias主要区别在于nginx如何解释location后面的uri,这会使两者分别以不同的方式将请求映射到服务器文件上。
root的处理结果是:root路径+location路径
alias的处理结果是:使用alias路径替换location路径
        if (!-e $request_filename) {
    
    
             rewrite ^ /api/index.php last;
        }
    }
}

Detailed explanation of nginx configuration priority

匹配优先级:精确匹配 >(^~) > 正则匹配 > 字符串(长 > 短)

= 精确匹配;
^~ 提高前缀字符串的匹配优先级;
~ 区分大小写的正则表达式匹配;
~* 不区分大小写的正则表达式匹配;
/ 通用匹配(因为所有的地址都以 / 开头,所以这条规则将匹配到所有请求),如果没有其它匹配,任何请求都会匹配到。


# 举例
location = /test.png {
    
    
    # 精确匹配
    [ configuration A ] 
}

location / {
    
    
    # 通用匹配,但是正则表达式和最长字符串会优先被匹配
    [ configuration B ] 
}

location /documents/ {
    
    
    # 前缀字符串匹配
    # 匹配任何以 /documents/ 开头的请求
    # 只有后面的正则表达式没有匹配到时,该配置才会被采用
    [ configuration C ] 
}

location ^~ /images/ {
    
    
    # 前缀字符串匹配
    # 匹配任何以 /images/ 开头的请求,匹配成功以后,会停止搜索后面的正则表达式匹配
    [ configuration D ] 
}

location ~* \.(gif|jpg|jpeg)$ {
    
    
    # 正则表达式匹配,匹配所有以 gif,jpg,jpeg 结尾的请求
    # 然而,所有请求 /images/ 下的图片会被 configuration D 处理,因为 ^~ 指令,匹配不到这一条规则
    [ configuration E ] 
}

location /images/abc/ {
    
    
    # 前缀字符串匹配
    # 只有去掉 configuration D 才能被匹配到
    [ configuration F ] 
}

Scan the QR code below, pay attention to the official account of "Yongkang Blog Network" to get more content

Guess you like

Origin blog.csdn.net/qq_36742250/article/details/116272934