首页 > 解决方案 > nginx 未知指令“return”

问题描述

我正在尝试使用股票代码在 nginx 中重定向:

    server {
       listen 80;
       server_name www.example.com;
       return 301 $scheme://example.com$request_uri;
    }

但是得到这个错误:

nginx unknown directive "return"

Nginx 是最新的,使用 apt install 安装在 ubuntu 上,而不是从源代码编译。

nginx version: nginx/1.19.0
built by gcc 9.3.0 (Ubuntu 9.3.0-10ubuntu2) 
built with OpenSSL 1.1.0h  27 Mar 2018
TLS SNI support enabled
configure arguments: --with-cc-opt='-g -O2 -fPIE -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -m64 -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=2 -march=native -mtune=native -fasynchronous-unwind-tables' --with-ld-opt='-Wl,-Bsymbolic-functions -fPIE -pie -Wl,-z,relro -Wl,-z,now' --error-log-path=/var/log/nginx/nginx_error --http-log-path=/var/log/nginx/nginx_log --user=nginx --group=nginx --sbin-path=/usr/local/sbin/nginx --conf-path=/etc/nginx/nginx.conf --pid-path=/var/run/nginx.pid --with-http_ssl_module --with-file-aio --with-openssl=/src/openssl --with-poll_module --with-http_stub_status_module --with-http_v2_module --with-threads --with-zlib=/src/zlib-1.2.11 --with-http_gzip_static_module --without-http_autoindex_module --without-http_browser_module --without-http_geo_module --without-http_memcached_module --without-http_ssi_module --without-http_userid_module --without-mail_pop3_module --without-mail_imap_module --without-mail_smtp_module --without-http_split_clients_module --without-http_uwsgi_module --without-http_scgi_module --without-http_limit_conn_module --without-http_rewrite_module --http-client-body-temp-path=/dev/shm/client_temp --http-proxy-temp-path=/dev/shm/proxy_temp --http-fastcgi-temp-path=/dev/shm/fastcgi_temp --http-uwsgi-temp-path=/dev/shm/uwsgi_temp --http-scgi-temp-path=/dev/shm/scgi_temp --add-module=/src/headers-more```

标签: nginx

解决方案


指令return包含在ngx_http_rewrite_module中。根据您的配置选项--without-http_rewrite_modulenginx,您正在运行编译而无需模块。

尝试从官方 repos重新安装 nginx或安装所需的模块


推荐阅读