Haproxy,Nginx Pretty Url 不起作用

Haproxy,Nginx Pretty Url 不起作用

你好,我在使用 Haproxy 后面的 nginx 设置漂亮的 URL 时遇到了麻烦。

Haproxy配置文件

global
    log /dev/log    local0
    log /dev/log    local1 notice
    chroot /var/lib/haproxy
    stats socket /run/haproxy/admin.sock mode 660 level admin
    stats timeout 30s
    user haproxy
    group haproxy
    daemon
    maxconn 10000

    # Default SSL material locations
    ca-base /etc/ssl/certs
    crt-base /etc/ssl/private

    # Default ciphers to use on SSL-enabled listening sockets.
    # For more information, see ciphers(1SSL).
    ssl-default-bind-ciphers kEECDH+aRSA+AES:kRSA+AES:+AES256:RC4-SHA:!kEDH:!LOW:!EXP:!MD5:!aNULL:!eNULL
    ssl-default-bind-options no-sslv3

defaults
        log     global
        mode    http
        option  httplog
        option  dontlognull
        option forwardfor
        option http-server-close
        timeout connect 5000
        timeout client  50000
        timeout server  50000
        errorfile 400 /etc/haproxy/errors/400.http
        errorfile 403 /etc/haproxy/errors/403.http
        errorfile 408 /etc/haproxy/errors/408.http
        errorfile 500 /etc/haproxy/errors/500.http
        errorfile 502 /etc/haproxy/errors/502.http
        errorfile 503 /etc/haproxy/errors/503.http
        errorfile 504 /etc/haproxy/errors/504.http
        stats enable
        stats uri /stats
        maxconn 2048
    frontend www-http
       bind 127.0.0.1:80

       reqadd X-Forwarded-Proto:\ http
       default_backend www-backend

    frontend www-https
       bind 127.0.0.1:443 ssl crt /etc/ssl/certs/server.pem

       reqadd X-Forwarded-Proto:\ https
       default_backend www-backend

    backend www-backend
       mode http
       redirect scheme https if !{ ssl_fc }
       server www-14547133 10.132.61.165:80 check # id:14547133, hostname:server-0
       server www-14559266 10.132.39.65:80 check # id:14559266, hostname:server-1

nginx.conf

user www-data;
worker_processes 4;
pid /run/nginx.pid;

events {
    worker_connections 768;
    # multi_accept on;
}

http {

    ##
    # Basic Settings
    ##

    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;
    # server_tokens off;

    # server_names_hash_bucket_size 64;
    # server_name_in_redirect off;

    include /etc/nginx/mime.types;
    default_type application/octet-stream;

    ##
    # Logging Settings
    ##

    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;

    ##
    # Gzip Settings
    ##

    gzip on;
    gzip_disable "msie6";

    # gzip_vary on;
    # gzip_proxied any;
    # gzip_comp_level 6;
    # gzip_buffers 16 8k;
    # gzip_http_version 1.1;
    # gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss te$

    ##
    # nginx-naxsi config
    ##
    # Uncomment it if you installed nginx-naxsi
    ##

    #include /etc/nginx/naxsi_core.rules;

    ##
    # nginx-passenger config
    ##
    # Uncomment it if you installed nginx-passenger
    ##

    #passenger_root /usr;
    #passenger_ruby /usr/bin/ruby;

    ##
    # Virtual Host Configs
    ##

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;

        server {
                include /usr/share/nginx/html/nginx.conf;
        }
}

/etc/nginx/sites-enabled/default

# You may add here your
# server {
#       ...
# }
# statements for each of your virtual hosts to this file

##
# You should look at the following URL's in order to grasp a solid understanding
# of Nginx configuration files in order to fully unleash the power of Nginx.
# http://wiki.nginx.org/Pitfalls
# http://wiki.nginx.org/QuickStart
# http://wiki.nginx.org/Configuration
#
# Generally, you will want to move this file somewhere, and start with a clean
# file but keep this around for reference. Or just disable in sites-enabled.
#
# Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.
##

server {
        listen 80 default_server;
        listen [::]:80 default_server ipv6only=on;

        root /usr/share/nginx/html;
        index index.php index.html index.htm;

        # Make site accessible from http://localhost/
        server_name localhost;

        location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                try_files $uri $uri/ =404;
                # Uncomment to enable naxsi on this location
                # include /etc/nginx/naxsi.rules
        }

        error_page 404 /404.html;
        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
                root /usr/share/nginx/html;
        }

        location ~ \.php$ {
                try_files $uri =404;
                fastcgi_split_path_info ^(.+\.php)(/.+)$;
                fastcgi_pass unix:/var/run/php5-fpm.sock;
                fastcgi_index index.php;
                include fastcgi_params;
        }

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #       deny all;
        #}
}

/usr/share/nginx/html/nginx.conf

location / {
    index            index.html index.htm index.php;
    try_files $uri.php $uri/ =404;
}

我正在尝试执行类似 /terms 到 /terms.php 的操作并隐藏扩展。

我对服务器设置还不熟悉。

任何帮助,将不胜感激。

谢谢!

答案1

存在许多问题。

该文件/usr/share/nginx/html/nginx.conf是服务器块的内容,目前还不够完整,无法执行任何有用的操作。

我建议/etc/nginx/sites-enabled/default包含当前运行您网站的服务器块。

看着这个文件有关服务器块的更多信息。

location /处理静态内容,location ~ \.php$块处理以 结尾的任何内容。因此,您尝试在块内使用.php运行 PHP 文件注定会失败。请参阅location /try_files这个文件了解更多信息try_files

您需要重命名您的$urito$uri.php并在 PHP 块中处理它。有很多方法可以实现这一点,但一种简单的方法是使用命名位置和重写。

location / {
    try_files $uri $uri/ @php;
}

location @php {
    rewrite ^ $uri.php last;
}

location ~ \.php$ {
    try_files $uri =404;
    fastcgi_pass unix:/var/run/php5-fpm.sock;
    include fastcgi_params;
}

原始块中的=404被替换为,这会导致调用内部重写。然后找到现有块。参见@phplocation /nginxlocation ~ \.php$欲了解更多信息,请访问locationrewrite

相关内容