简体   繁体   English

重写Nginx和Codeigniter的规则

[英]rewrite rules for nginx and Codeigniter

I have implemented a php application in codeigniter and now want to deploy it to the nginx server. 我已经在codeigniter中实现了一个php应用程序,现在想将其部署到nginx服务器。 Before deploying I checked my nignx configuration on my localhost using MAMP server. 部署之前,我使用MAMP服务器在本地主机上检查了nignx配置。 It is working correctly. 它工作正常。 But, this configuration is not working on the live server. 但是,此配置在实时服务器上不起作用。 As a beginner in nginx, I am not understanding where is the mistake here. 作为nginx的初学者,我不明白这里的错误在哪里。 In live server, I can not write in the main nginx.conf file. 在实时服务器中,我无法写入主nginx.conf文件。 I have a separate configuration file like "abc" for my application "abc". 我的应用程序“ abc”有一个单独的配置文件,如“ abc”。 And all my application files are under "abc/xyz" directory. 我所有的应用程序文件都在“ abc / xyz”目录下。 Here is my sample confuguration, 这是我的样本配置,

location /abc {
root /srv/www/htdocs/apps/;

index index.html index.htm index.php;


location /xyz {
    try_files $uri $uri/ /abc/xyz/index.php;
}

location ~ \.php(\/(\w+))*$ {
    try_files $uri =404;
    rewrite (.+)\.php(\/(\w+))*$ $1.php break;

    include /etc/nginx/fastcgi_params;

    fastcgi_index index.php;
    fastcgi_pass unix:/var/run/php-fpm.sock;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}

} }

Here, I can see my welcome page https://myapplication/abc/xyz . 在这里,我可以看到我的欢迎页面https:// myapplication / abc / xyz But if I want to navigate other pages like https://myapplication/abc/xyz/other_pages , it is showing "404 Page not found". 但是,如果我要浏览其他页面,例如https:// myapplication / abc / xyz / other_pages ,则会显示“ 404页面未找到”。 I have checked the other solutions but none of them is not working in this case. 我已经检查了其他解决方案,但是在这种情况下它们都不起作用。 Thanks in advance for the help! 先谢谢您的帮助!

The location /xyz block is nested within the location /abc block. location /xyz块嵌套在location /abc块内。 The nested block is required to precess URIs with a prefix of /abc/xyz . 需要嵌套块来处理具有/abc/xyz前缀的URI。

If there are other regular expression location blocks surrounding your location /abc block , you should use the ^~` modifier. 如果您的location /abc块周围还有其他正则表达式 location, you should use the ^〜`修饰符。

For example: 例如:

location ^~ /abc {
    ...
    location /abc/xyz {
        ...
    }
    ...
}

See this document for more. 有关更多信息,请参见此文档

Sorry for the late answer. 抱歉回复晚了。 It was actually very silly mistake. 这实际上是非常愚蠢的错误。 My controller page name was in small character. 我的控制器页面名称是小写字母。 This is why it was not working. 这就是为什么它不起作用。 My configuration is okay. 我的配置还可以。 The first letter of the controller page should be in capital character. 控制器页面的首字母应为大写字母。 For example, my controller name is Home. 例如,我的控制器名称是Home。 So my php file name must be Home.php not home.php. 所以我的php文件名必须是Home.php而不是home.php。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM