我在不同的路由上设置了两个 laravel 后端。并且根目录上有一个 spa。现在我想设置一个 websocket 服务器。这是我的 website.conf
############## block-4 : multiple subdirectory testing ############
server {
listen 80;
# server_name abc.xyz;
server_name _;
root /var/www/html/abc.xyz;
# root /var/www/html;
add_header X-Frame-Options "SAMEORIGIN";
add_header X-Content-Type-Options "nosniff";
index index.html index.php;
location / {
# root /var/www/html/abc.xyz;
try_files $uri /index.html ;
}
charset utf-8;
location = /favicon.ico {
access_log off; log_not_found off;
}
location = /robots.txt {
access_log off; log_not_found off;
}
# error_page 404 /index.php;
############## block-4 : multiple subdirectory testing ############
server {
listen 80;
# server_name abc.xyz;
server_name _;
root /var/www/html/abc.xyz;
# root /var/www/html;
add_header X-Frame-Options "SAMEORIGIN";
add_header X-Content-Type-Options "nosniff";
index index.html index.php;
location / {
# root /var/www/html/abc.xyz;
try_files $uri /index.html ;
}
charset utf-8;
location = /favicon.ico {
access_log off; log_not_found off;
}
location = /robots.txt {
access_log off; log_not_found off;
}
# error_page 404 /index.php;
# BACKEND location rewrite instructions
location /backend {
alias /var/www/html/abc.xyz/backend;
try_files $uri $uri/ @backend;
location ~ \.php$ {
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $request_filename;
fastcgi_pass unix:/var/run/php/php8.0-fpm.sock;
}
}
location @backend {
# rewrite /backend/(.*)$ /backend/index.php?/$1 last;
rewrite ^/backend/(.*)$ /backend/index.php last;
}
# end of the BACKEND location
# BACKEND location rewrite instructions
location /api {
alias /var/www/html/abc.xyz/api;
try_files $uri $uri/ @api;
location ~ \.php$ {
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $request_filename;
fastcgi_pass unix:/var/run/php/php8.0-fpm.sock;
}
}
location @api {
rewrite /api/(.*)$ /api/index.php?/$1 last;
}
# end of the BACKEND location
# phpmyadmin rewrite rules.
location /phpmyadmin {
root /usr/share/;
index index.php index.html index.htm;
location ~ ^/phpmyadmin/(.+\.php)$ {
try_files $uri =404;
root /usr/share/;
fastcgi_param SCRIPT_FILENAME $request_filename;
fastcgi_pass unix:/var/run/php/php8.0-fpm.sock;
fastcgi_index index.php;
include /etc/nginx/fastcgi_params;
}
location ~* ^/phpmyadmin/(.+\.(jpg|jpeg|gif|css|png|js|ico|html|xml|txt))$ {
root /usr/share/;
}
}
# end of phpmyadmin block here.
# web socket configuration here
location /ws* {
proxy_pass http://127.0.0.1:6001;
proxy_set_header Host $host;
proxy_read_timeout 60;
proxy_connect_timeout 60;
proxy_redirect off;
# Allow the use of websockets
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
# end of websocket configuration
location ~ /\.(?!well-known).* {
deny all;
}
}
############# end block-4 ######################
该location /ws
块取自 laravel websockets 文档。但此位置块给出 404 未找到错误。但这个 nginx 配置不起作用。我的 laravel 网站项目在 localhost 上运行良好。
编辑
这是输出lsof -i :80,443,6001
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
php 414490 root 5u IPv4 5974385 0t0 TCP *:x11-1 (LISTEN)
php 414490 root 6u IPv4 5991457 0t0 TCP localhost.localdomain:x11-1->localhost.localdomain:35684 (ESTABLISHED)
nginx 415533 root 6u IPv4 5990824 0t0 TCP *:https (LISTEN)
nginx 415533 root 7u IPv4 5990825 0t0 TCP *:http (LISTEN)
nginx 415534 www-data 6u IPv4 5990824 0t0 TCP *:https (LISTEN)
nginx 415534 www-data 7u IPv4 5990825 0t0 TCP *:http (LISTEN)
nginx 415535 www-data 6u IPv4 5990824 0t0 TCP *:https (LISTEN)
nginx 415535 www-data 7u IPv4 5990825 0t0 TCP *:http (LISTEN)
nginx 415536 www-data 6u IPv4 5990824 0t0 TCP *:https (LISTEN)
nginx 415536 www-data 7u IPv4 5990825 0t0 TCP *:http (LISTEN)
nginx 415537 www-data 6u IPv4 5990824 0t0 TCP *:https (LISTEN)
nginx 415537 www-data 7u IPv4 5990825 0t0 TCP *:http (LISTEN)
nginx 415538 www-data 6u IPv4 5990824 0t0 TCP *:https (LISTEN)
nginx 415538 www-data 7u IPv4 5990825 0t0 TCP *:http (LISTEN)
nginx 415539 www-data 6u IPv4 5990824 0t0 TCP *:https (LISTEN)
nginx 415539 www-data 7u IPv4 5990825 0t0 TCP *:http (LISTEN)
nginx 415539 www-data 8u IPv4 5985866 0t0 TCP linux:https->157.42.56.21:55728 (ESTABLISHED)
nginx 415539 www-data 12u IPv4 5985868 0t0 TCP localhost.localdomain:35684->localhost.localdomain:x11-1 (ESTABLISHED)