2017-01-02 67 views
0

時,服務器顯示404將不勝感激。當使用https [nginx]

我已經收到了letsencrypt的SSL證書,並且正在嘗試爲我的服務器同時支持HTTP和HTTPS。該證書是好的,但當我試圖使用https訪問我的網站時,我收到了404的證書。這裏是我的配置文件:

server { 
    server_name my.domain.org; 

    root /var/www/my.domain.org/htdocs; 
    index index.php index.html index.html; 

    server_name my.domain.org; 
    location/{ 
    try_files $uri $uri/ /index.php?$args; 
    } 

    # Add trailing slash to */wp-admin requests. 
    rewrite /wp-admin$ $scheme://$host$uri/ permanent; 

    location ~ \.php$ { 
    fastcgi_split_path_info ^(.+\.php)(/.+)$; 
    # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini 

    # # With php5-cgi alone: 
    # fastcgi_pass 127.0.0.1:9000; 
    # With php5-fpm: 
    fastcgi_pass unix:/var/run/php5-fpm.sock; 
    fastcgi_index index.php; 
    include fastcgi_params; 
    } 

    location = /favicon.ico { 
    access_log off; 
    log_not_found off; 
    } 

    location = /robots.txt { 
    access_log off; 
    log_not_found off; 
    } 

    location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ { 
    expires max; 
    log_not_found off; 
    } 
} 

server { 
    listen 443; 

    ssl on; 
    ssl_certificate /etc/letsencrypt/live/my.domain.org/fullchain.pem; 
    ssl_certificate_key /etc/letsencrypt/live/my.domain.org/privkey.pem; 

    server_name my.domain.org; 
    access_log /var/log/nginx/nginx.vhost.access.log; 
    error_log /var/log/nginx/nginx.vhost.error.log; 

    location/{ 
    root /var/www/my.domain.org/htdocs; 
    index index.php index.html index.html; 
    } 
} 

回答

0

https的配置與http沒有區別。

所以,只需要複製相同的HTTP配置,並添加SSL特定部分:

# https 
server { 
    listen 443 ssl; 
    # for http2 support uncomment line bellow and comment line above 
    # listen 443 ssl http2; 

    ssl on; 
    ssl_certificate /etc/letsencrypt/live/my.domain.org/fullchain.pem; 
    ssl_certificate_key /etc/letsencrypt/live/my.domain.org/privkey.pem; 

    server_name my.domain.org; 

    root /var/www/my.domain.org/htdocs; 
    index index.php index.html index.html; 

    location/{ 
    try_files $uri $uri/ /index.php?$args; 
    } 

    rewrite /wp-admin$ $scheme://$host$uri/ permanent; 

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

    location = /favicon.ico { 
    access_log off; 
    log_not_found off; 
    } 

    location = /robots.txt { 
    access_log off; 
    log_not_found off; 
    } 

    location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ { 
    expires max; 
    log_not_found off; 
    } 
} 

#http 
server { 
    listen 80; 
    root /var/www/my.domain.org/htdocs; 
    index index.php index.html index.html; 

    server_name my.domain.org; 
    location/{ 
    try_files $uri $uri/ /index.php?$args; 
    } 

    rewrite /wp-admin$ $scheme://$host$uri/ permanent; 

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

    location = /favicon.ico { 
    access_log off; 
    log_not_found off; 
    } 

    location = /robots.txt { 
    access_log off; 
    log_not_found off; 
    } 

    location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ { 
    expires max; 
    log_not_found off; 
    } 
} 
+0

謝謝你,我很欣賞這一點。還有一個問題需要澄清,你在這裏粘貼的是我的整個配置文件? (忽略其他服務器{}塊)或者這隻會替換我現在用於SSL的塊? – nginthrowaway

+0

@nginthrowaway我已經更新了我的答案您可以複製全部 – num8er

+1

像魅力一樣工作,非常感謝! – nginthrowaway