Nginx 单域名多项目配置汇总

本篇文章为自用nginx多项目配置汇总,涉及Laravel项目/Websocket/Vue项目和Golang项目,基础项目为laravel项目。

基础项目配置

laravel的nginx配置。

访问:***.test.cn

server
{
	listen 443 ssl http2;
	listen 80;
    server_name 139.***.***.** ***.test.cn;
    index index.php index.html index.htm default.php default.htm default.html;
    root /data/test/public;
    
    #SSL-START SSL相关配置,请勿删除或修改下一行带注释的404规则
    #error_page 404/404.html;
    #HTTP_TO_HTTPS_START
    if ($server_port !~ 443){
        rewrite ^(/.*)$ https://$host$1 permanent;
    }
    #HTTP_TO_HTTPS_END
    ssl_certificate    /www/server/panel/vhost/cert/***.test.cn/fullchain.pem;
    ssl_certificate_key    /www/server/panel/vhost/cert/***.test.cn/privkey.pem;
    ssl_protocols TLSv1.1 TLSv1.2 TLSv1.3;
    ssl_ciphers EECDH+CHACHA20:EECDH+CHACHA20-draft:EECDH+AES128:RSA+AES128:EECDH+AES256:RSA+AES256:EECDH+3DES:RSA+3DES:!MD5;
    ssl_prefer_server_ciphers on;
    ssl_session_cache shared:SSL:10m;
    ssl_session_timeout 10m;
    add_header Strict-Transport-Security "max-age=31536000";
    error_page 497  https://$host$request_uri;

    #SSL-END
    
    #ERROR-PAGE-START  错误页配置,可以注释、删除或修改
    #error_page 404 /404.html;
    #error_page 502 /502.html;
    #ERROR-PAGE-END
    
    #PHP-INFO-START  PHP引用配置,可以注释或修改
    include enable-php-73.conf;
    #PHP-INFO-END
    
    # 伪静态
	location / {  
		try_files $uri $uri/ /index.php$is_args$query_string;  
	}
    
    #禁止访问的文件或目录
    location ~ ^/(\.user.ini|\.htaccess|\.git|\.svn|\.project|LICENSE|README.md)
    {
        return 404;
    }
    
    #一键申请SSL证书验证目录相关设置
    location ~ \.well-known{
        allow all;
    }
    
    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
    {
        expires      30d;
        error_log /dev/null;
        access_log /dev/null;
    }
    
    location ~ .*\.(js|css)?$
    {
        expires      12h;
        error_log /dev/null;
        access_log /dev/null; 
    }
    access_log  /www/wwwlogs/***.test.cn.log;
    error_log  /www/wwwlogs/***.test.cn.error.log;
}

laravel多项目

访问:***.test.cn/lxw-p1/

# project1开始的配置
location ^~ /lxw-p1/ {
	alias /data/p1/public/;
	try_files $uri $uri/ @lxw-p1;
	location ~ \.php$ {
			fastcgi_pass unix:/tmp/php-cgi-73.sock;
			fastcgi_index index.php;
			fastcgi_param SCRIPT_FILENAME $request_filename;
			include fastcgi_params;
	}
	
}

location @lxw-p1 {
	rewrite /lxw-p1/(.*)$ /lxw-p1/index.php?/$1 last;
}

# project2开始的配置
location ^~ /lxw-p2/ {
	alias /data/p2/public/;
	try_files $uri $uri/ @lxw-p2;
	location ~ \.php$ {
			fastcgi_pass unix:/tmp/php-cgi-73.sock;
			fastcgi_index index.php;
			fastcgi_param SCRIPT_FILENAME $request_filename;
			include fastcgi_params;
	}
	
}

location @lxw-p2 {
	rewrite /lxw-p2/(.*)$ /lxw-p2/index.php?/$1 last;
}

WebSocket/Golang多项目

访问:***.test.cn/ws-or-go/

# project 1
location /lxw-ws1 {   
	  #host修改为真实的域名和端口
	  proxy_set_header   Host             $http_host;
	  #客户真实ip
	  proxy_set_header   X-Real-IP        $remote_addr;
	  proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
	  #客户端真实协议
	  proxy_set_header   X-Forwarded-Proto  $scheme;
	  proxy_set_header Upgrade $http_upgrade;
	  proxy_set_header Connection "upgrade";
	  rewrite ^/ws1/(.*)$ /$1 break;
	  proxy_pass  http://127.0.0.1:8026;   #设置代理服务器的协义和地址
}

# project 2
location /lxw-go2 {   
	  #host修改为真实的域名和端口
	  proxy_set_header   Host             $http_host;
	  #客户真实ip
	  proxy_set_header   X-Real-IP        $remote_addr;
	  proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
	  #客户端真实协议
	  proxy_set_header   X-Forwarded-Proto  $scheme;
	  proxy_set_header Upgrade $http_upgrade;
	  proxy_set_header Connection "upgrade";
	  rewrite ^/ws2/(.*)$ /$1 break;
	  proxy_pass  http://127.0.0.1:8027;   #设置代理服务器的协义和地址
}

Vue多项目

访问:***.test.cn/p1/index.html

# project 1
location ^~/p1 {
	alias /data/vue-p1;
	try_files $uri $uri/ /p1/index.html;
	index index.html;
}

# project 2
location ^~/p2 {
	alias /data/vue-p2;
	try_files $uri $uri/ /p2/index.html;
	index index.html;
}

你可能感兴趣的:(服务器,nginx,运维,laravel,vue,websocket)