我尝试使用 nuxtjs 作为前端,使用 laravel 作为后端,并使用 NGINX
添加 ssl 并重新配置 nginx .conf 文件后,现在我的error_page
线路不工作了,它没有转到location @php
,而是抛出了一个前端 404(nuxt 错误页面)
server{
listen 45.xx.xxx.xxx:80;
server_name example.com www.example.com;
root /home/example/core/public/;
return 301 https://www.example.com$request_uri;
}
server {
listen 45.xx.xxx.xxx:443 ssl;
# NOTE: SSL configuration is defined elsewhere
server_name example.com;
ssl_certificate /etc/pki/tls/certs/example.com.cert;
ssl_certificate_key /etc/pki/tls/private/example.com.key;
return 301 https://www.example.com$request_uri;
}
server {
listen 45.xx.xxx.xxx:443 ssl;
server_name www.example.com;
ssl_certificate /etc/pki/tls/certs/example.com.cert;
ssl_certificate_key /etc/pki/tls/private/example.com.key;
root /home/rabter/core/public/;
index index.php;
access_log /var/log/nginx/example.com.bytes bytes;
access_log /var/log/nginx/example.com.log combined;
error_log /var/log/nginx/example.com.error.log error;
ssl_protocols SSLv3 TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
ssl_ciphers 'EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH';
location / {
root /home/example/core/public/;
index index.php;
proxy_set_header Connection "keep-alive";
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_http_version 1.1;
proxy_pass https://45.xx.xxx.xxx:3000$request_uri;
error_page 404 = @php;
}
location @php {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_param HTTPS $https;
fastcgi_pass 45.xx.xxx.xxx:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_intercept_errors off;
fastcgi_buffer_size 16k;
fastcgi_buffers 4 16k;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
}
}
在添加 ssl 之前,error_page 404 = @php
运行正常,并且进行了 api 调用。看起来 nginx 忽略了 error_page 行。感谢您的帮助
答案1
正确的 SSL 配置文件
http 配置文件
server {
listen your-server-ip:80;
server_name example.com;
return 301 https://www.example.com$request_uri;
}
ssl 配置文件
server {
listen your-server-ip:443 ssl;
server_name example.com;
return 301 https://www.example.com$request_uri;
}
server {
listen your-server-ip:443 ssl;
server_name www.example.com;
ssl_certificate /etc/pki/tls/certs/example.com.bundle;
ssl_certificate_key /etc/pki/tls/private/example.com.key;
root /home/example/core/public/;
index index.php;
access_log /var/log/nginx/example.com.bytes bytes;
access_log /var/log/nginx/example.com.log combined;
error_log /var/log/nginx/example.com.error.log error;
location / {
proxy_set_header Connection "keep-alive";
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_http_version 1.1;
proxy_pass https://your-server-ip:3000$uri;
proxy_intercept_errors on;# In order to use error_page directive this needs to be on
error_page 404 = @php;
}
location @php {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass your-server-ip:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_intercept_errors off;
fastcgi_buffer_size 16k;
fastcgi_buffers 4 16k;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
}
}
请记住,我的端口3000
可能与您的不同
fastcgi_pass 也可以指向一个 sock,但我直接添加了它
在我的例子中,fastcgi_pass 端口设置为 9000,您的可能有所不同
话虽如此,我们正在重定向 80 和 433没有 www到万维网然后进行反向代理,如果反向代理为 404,我们尝试使用 @php,实际上我们使用 php-fpm 来运行我们的 php 代码
我花了差不多两周的时间学习 nginx 并编写不同的配置,直到我想出了这个