学无先后,达者为师

网站首页 编程语言 正文

nginx 反向代理以及 location /admin/

作者:Blau 更新时间: 2022-10-14 编程语言

nginx 反向代理以及 location /admin/


反向代理时, 需要特别注意,本地测试时确保类似IP调用 127.0.0.1:8082/admin 这种情况可以直接访问,在做反向代理。否则请使用测试域名访问,反向代理才可以成功,最好设置相关header头,跳转的才可以使用。

// 常规
server {
    listen 8280;
    server_name angelsteward.test *.angelsteward.test;
    root "E:/own/git/angelsteward/public/";
    
    index index.html index.htm index.php;
 
    location / {
        try_files $uri $uri/ /index.php$is_args$args;
		autoindex on;
    }
    
    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass php_upstream;		
        #fastcgi_pass unix:/run/php/php7.0-fpm.sock;
    }
	
	
    charset utf-8;
	
    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt  { access_log off; log_not_found off; }
    location ~ /\.ht {
        deny all;
    }
}

// 反向代理
server {
    listen 8280;
    server_name angelsteward.test *.angelsteward.test;
 
    location / {
        root "C:\Users\Y\Desktop\18token\18token.im";
		index index.html index.htm index.php; 
		autoindex on;
    }
	
	 location /admin { 
        #alias "E:/own/git/cim/public/"; 
		proxy_pass  http://127.0.0.1:8082;
		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;
		#try_files $uri $uri/ /index.php$is_args$args;
        #index index.html index.htm index.php; 
        #autoindex on; 
    } 
    
    location ~ \.php(.*)$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass php_upstream;		
        #fastcgi_pass unix:/run/php/php7.0-fpm.sock;
    }
	
	
    charset utf-8;
	
    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt  { access_log off; log_not_found off; }
    location ~ /\.ht {
        deny all;
    }
}

// ssh 443 https
server {
	listen       4430  ssl;
#	listen       443;
    server_name  angelsteward.test *.angelsteward.test;
#	ssl                  on;
    ssl_certificate      E:\own\git\https\server.pem;
    ssl_certificate_key  E:\own\git\https\privkey.pem;
    ssl_session_timeout  5m;
    ssl_protocols  SSLv2 SSLv3 TLSv1;
    ssl_ciphers  HIGH:!aNULL:!MD5;
    ssl_prefer_server_ciphers   on;
	
	root "E:/own/git/angelsteward/public/";
    
    index index.html index.htm index.php;
 
    location / {
        try_files $uri $uri/ /index.php$is_args$args;
		autoindex on;
    }
    
    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass php_upstream;		
        #fastcgi_pass unix:/run/php/php7.0-fpm.sock;
    }
	
	
    charset utf-8;
	
    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt  { access_log off; log_not_found off; }
    location ~ /\.ht {
        deny all;
    }
}

# This file is auto-generated.
# If you want Laragon to respect your changes, just remove the [auto.] prefix
# If you want to use SSL, enable it at: Menu > Nginx > SSL > Enabled

重点 alias 关键字,在此处是通过 / 根目录访问静态网页。/admin 访问 php 项目,此处由于原 laravel 项目域名路由已经定义 admin 路径冲突,只能使用反向代理配置。
这种情况应该是适用于 php 项目已有 /admin 子目录时且已经是适配 /admin 的路由,或者是类似 /api 的路由配置,才可以进行目录跳转适配。否则产生错误404。

即反向代理用户多个不同的项目部署于一个域名下。
alias 用于项目(多个)下的多个模块区分,比如 前后端、api 分离 的路由指定。


在做 https ssh 443 的配置时,暂可以忽略其他配置。

原文链接:https://blog.csdn.net/weixin_43930641/article/details/123066255

栏目分类
最近更新